The line single_true_false = digits // 10**single % 10 is giving me an
unsupported operand type(s) for //: ‘str’ and ‘int’
exception. Does anyone know how to resolve this?
Also, is there a more pythonic or productive way to write my code in general? I am new to python so I would really appreciate the help
with open("test.txt", "r") as file:
open_file = file.read()
satisfied = 0
not_satisfied = 0 #still need to check if it is alpha!!!!
distinct = {x.rstrip() for x in open_file} #distinct = (variable for variable in open_file if not variable in distinct)
length = len(distinct) #list
for i in range(0,2**length):
binary = bin(i)
removed = binary[2:] #removing "0b" prefix
digits = f"removed:length"
true_false = ()
for single in range(0,length): #See slide 24
single_true_false = digits // 10**single % 10
true_false.append(single_true_false)
environments = zip(distinct,true_false)
if eval(open_file,environments):
satisfied += 1
else:
not_satisfied += 1
print('Satisfied: ', satisfied, '; Not Satisfied: ', not_satisfied)
>Solution :
The immediate cause of the error is simple: digits = f"removed:length" assigns the string value "removed:length" to digits. You can’t use that as an operand to an arithmetic operation.
Presumably, since you preceded the string literal with f, you wanted to substitute values into the string. For instance you might have meant to write digits = f"{removed}:{length}". The curly braces indicate to Python where you want substitution to occur.
However, that still wouldn’t give you an integer value, and I don’t know what integer value you would expect it to give you, given the colon in the middle of the string. What do you want digits to contain?