I have a program that's supposed to calculate Hamming Code for even parity with a 7-bit integer, here is the program:
data=list(input("Enter a 7-bit binary integer:"))
if (data[0]+data[1]+data[3]+data[4]+data[6])%2 == 0:
data.insert(8, "0")
else:
data.insert(8, "1")
if (data[0]+data[2]+data[3]+data[5]+data[6])%2 == 0:
data.insert(7, "0")
else:
data.insert(7, "1")
if (data[1]+data[2]+data[3])%2 == 0:
data.insert(6, "0")
else:
data.insert(6, "1")
if (data[4]+data[5]+data[6])%2 == 0:
data.insert(3, "0")
else:
data.insert(3, "1")
print("Your 7-bit binary integer with Hamming Code parity bits:",data)
However, when I run this program I get this error:
Traceback (most recent call last):
File "C:\Python34\hamcode.py", line 3, in <module>
if (data[0]+data[1]+data[3]+data[4]+data[6])%2 == 0:
TypeError: not all arguments converted during string formatting
I'm not sure what this means and how to fix it, any responses would be greatly appreciated.
The type of
data
is a list with strings, not a list of integers:As such, you're trying to concatenate strings and you're not summing numbers as expected:
To fix it, you'll need to change all the strings into numbers first:
At the moment this line is adding the strings in the list back together to a single string and you're trying to use string formatting on that string (with
% 2
which the syntax for string formatting). The operator%
is the modulo operator when applied to a number but it's the string formatting operator when applied to a string.In other words, you're doing:
which means Python is trying to insert that
2
into the string123456
at the appropriate place (which isn't possible because there is no place designated for it).