I am writing a relatively simple program using Tkinter that is a converter for binary, decimal and hex. I am not using any of the Python built in math functions since I figured this was a good way to get some practice with, not only classes and objects, but Tkinter as well.
I have a method that checks to make sure a binary string is valid by checking if it contains any of the digits 2-9, which would make it invalid.
To do this I am using an if statement embedded within a for loop
bits = '011101' #self.input_str.get()
bit_list = [i for i in bits]
ill_bits = ['2', '3', '4', '5', '6', '7', '8', '9']
for bit in bit_list:
if bit in ill_bits:
print('yes')
#self.output_disp.delete(0.0, END)
#self.output_disp.insert(0.0, "That bit string is invalid")
else:
print('no')
#self.from_binary(self.dec_bttn, self.hex_bttn)`
The two print() functions are actually not part of the program, they are there as I keep testing different things. The commented out parts are the code that I want to run in the test area of the Tkinter app as results of the validity test.
What is happening here is that the
self.output_disp.delete(0.0, END)
self.output_disp.insert(0.0, "That bit string is invalid")
is only run if the very last bit in the test is found in both lists. For example a bit string of: 111019 will have That bit string is invalid shown in the text area, but 111911 will not. Although both bit strings will print yes in the console when the 9 is iterated through, only the one with the 9 in the last spot will run the two lines of code that start self.output_disp….
Your flow is a little off. You need to break after the first failure, and have the
elseblock associated with theforinstead of theif.