I’ve been going through a beginning Python book, and I’ve been trying to write a small block of code that will take users input, check to make sure it can be converted to an int and check if it’s higher than 49152.
I know there’s an easier way to do this, but I can’t get my mind to figure it out.
port_input = raw_input("port(number must be higher than 49152: ")
check = True
while check == True:
check = False
try:
port_number = int(port_input)
except:
port_input = raw_input("port(number must be higher than 49152: ")
check = True
while int(port_input) < 49152:
port_input = raw_input("Please enter a higher number(hint: more than 49152): ")
What you have isn’t functionaly correct anyway. Consider if someone puts “123” then “abc”. The 123 will get them through the
while checkblock, but when they get to thewhile < 49152block there’s no checking.Here’s what I come up with (I don’t do python, I just hacked it in based on your existing code…)