+ 2
How do I make sure that 01234 != 1234?
My Pin Access code has this problem. Try to Input 01234 to it and it will state Access Granted yet the pin has to be 1234. This is not applicable in the real world. Please guide me to how I would approach this problem. Here is the code: https://code.sololearn.com/cxcbnAGSQVVc/?ref=app
6 ответов
+ 2
The zero is only a problem if you convert the input into a number. You don't have to do that.
For example you can generate letters instead of real digits to begin with.
from random import randint
pin = ''
for i in range(4):
pin += chr(randint(48, 57))
print(pin)
The ASCII characters from 48 to 57 are the ten digits. You create a string of randomized digit 'letters'.
+ 4
Do you need the pin as a real number? You don't do calculations with it, do you?
I'd just leave it as string.
+ 3
0 will retain no value and is redundant unless if its in decimal place or after 1234. do this instead
pin = input()
try:
if pin != "1234":
print("Access Not Granted")
elif pin == "1234":
print ("Access Granted")
print ("Welcome")
except (ValueError, TypeError) :
print ("Access Not Granted")
+ 1
HonFu No, I don't plan on doing any calculations. The next step would be to make the code output a new pin everytime someone inputs the wrong pin and thus the new pin becomes the actual pin and the previous one is nullified. Now you see my problem if 0 will be accepted as pin in my code. Because one, the pin has to be four digits and two the pin for starters has to be 1234 before it is randomised.
+ 1
HonFu I will be sure to come back here when coding the random part of the code. And now I see the logic of using a string if at all I wont be making any calcs. Thank you once again!