A couple of things:
Start using range
.
Range is the recommended way to loop in Python.
And you can change your shutdown sequence, and while loop to use it.
This improves the clarity of your code, which is good for when others read it.
And even if no-one else will, it's always good to get good habits.
Reduce the amount of unneeded messages to the end user.
Here less is more.
Seriously, I was used to overly verbose messages when I was on windows.
And then I installed Linux and would most of the time get no feedback.
To change my keyboard layout I use loadkeys dvorak
, and I get no message.
I create a file, touch file
, and again no message.
The only time I get a message is if that's the intent of the command, ls
, or if it's an error.
Honestly you should probably take a leap out of their book.
It's normally best to avoid recursion.
I once made a program that heavily used recursion,
it exceeded the recursion limit and there was no way for me to fix it.
I can assure you I never touched that project again.
Instead I'd use a while loop.
Use getpass
.
This removes the need to display a lot of hashes to remove the password from display.
As it never displays in the first place.
Change password
to create the password.
This way the method of creating a password is always the same.
Change login
to take the password as an argument.
This is as it removes reliance on globals which is always a good thing.
You should check the input is a number, otherwise you program will exit with an error.
from time import sleep
from getpass import getpass # 4
def create_password(): # 5
password = getpass('Create your password: ') # 4, 5
try: # 7
password = float(password)
except ValueError:
quit()
if password > 9999:
print('Password is invalid, initiating shutdown.')
for i in range(5, -1, -1): # 1
time.sleep(1)
print(i)
quit()
return password # 5
def login(password): # 6
while True: # 3
password_guess = float(input('Please enter password: '))
if password_guess == password:
break # 3
else:
print("Wrong. Here's a clue", (password - password_guess) ** 2)
password = create_password()
login(password)
The above shows how I'd change your code if I were to keep the messages you added special code for.
I still think most of your messages are not needed, and so I'd remove more of them.
I also dislike your clue, as it's ridiculously insecure.
And due to floating point inaccuracy I'd store the password as a string.
Resulting in:
from getpass import getpass
def create_password(): # 5
password = getpass('Create your password: ')
try:
float(password)
except ValueError:
quit()
if float(password) > 9999:
quit()
return password
def login(password):
while True:
password_guess = input('Please enter password: ')
if password_guess == password:
break
password = create_password()
login(password)
Finally I'd think about removing the float(password) > 9999
.