Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"'int' object is not iterable" – How to iterate over an integer?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading