Reputation: 47
I've looked for a solution to this online but haven't found anything relevant. An assignment for my course requires me to make a program where the computer will try to guess a number the user has input. The number needs to be between 001 and 100, so I have started by trying to ensure the user can only input numbers within that range.
The code i have so far:
import random
code=(int(input("Input a three digit code. Must be more than 001 and less than 100.")))
print(code)
if (code < 001) or (code > 100):
print ("Invalid code")
elif (code > 001) or (code < 100):
print ("Valid code")
It works fine with 001 changed to 1, but if i run it with 001 i get an Invalid Token error. This needs to be done in Python 3.4.3. Any sort of help would be greatly appreciated.
Upvotes: 3
Views: 694
Reputation: 336458
Integers can't have leading zeroes in Python. Leading zeroes were previously (Python 2) used to denote octal numerals. Since many people didn't know that and were confused why 070 == 56
, Python 3 made leading zeroes illegal.
You shouldn't be converting code
to an integer - only store actual numbers (that you intend to do calculations with) in numerical variables. Keep the string:
while True:
code = input("Input a three digit code. Must be more than 001 and less than 100.")
try:
value = int(code)
except ValueError:
print("Invalid code")
continue
if 1 <= value <= 100:
print ("Valid code")
break
else:
print ("Invalid code")
Upvotes: 3