Im a beginner in CS and I'm trying to add a pin elect to a simple ATM program. The first time a wrong pin is entered it prints the invalid pin message but the second time, the ways I have tried, it either accepts the wrong pins or stop when the wrong pin is entered. My goal is for it to keep asking for the pin until the correct pin is entered. Iv tried making it an if statement and a while true statement any tips are very much appreciated
# Constants for choice options
BALANCE = 1
DEPOSIT = 2
WITHDRAW = 3
QUIT = 4
pin = 1234
def main():
print("Welcome to STAR ATM!")
pin = (int(input('\nPlease enter your pin number: ')))
while pin == 1234:
showMenu()
else:
print('\nWrong pin please try again')
(input('\nPlease enter your pin number: '))
This isn't the entire program, this is just the pin part but let me know if I should post the whole thing
CodePudding user response:
Instead of having while pin == 1234
change the code to say while pin != 1234
which loops until pin is equal to 1234
. This way you can retry the pin as many times as you like until it is correct. To improve this, you can create a second variable pin_attempt
which is the pin which the user will add in, this way you can have the loop While pin_attempt != pin:
This way you can change the pin variable any time without having the change all instances of it.
Also, you should not change the variable pin
itself as that holds the pin
. If you do change the variable pin
, there is no point declaring it as 1234
since you immediately change what is stored before the variable can even be used. Thus having pin = 1234
would be useless.
You should also store the pin
as a string
instead of an integer
. This is because the string
is a password which is set rather than an integer
which can change.
# The credit card's PIN
pin = '1234'
# First attempt at the pin
pin_attempt = input('What is your pin?: ')
# If pin_attempt was incorrect this loop will run
while pin_attempt != pin:
# Enter the new attempt, after the new attempt will be checked
# If the new attempt is not equal to pin, the code will loop
pin_attempt = input('That pin is incorrect, try again: ')
# Any code that you want to run once the pin is entered correctly goes here
You can take this one step further by adding a counter decrements for every guess. If the number of guesses they have runs out they can no longer guess.
# The credit card's PIN
pin = '1234'
# Guess counter
counter = 3
# First attempt at the pin
pin_attempt = input('What is your pin?: ')
# If pin_attempt was incorrect this loop will run
while pin_attempt != pin:
# Enter the new attempt, after the new attempt will be checked
# If the new attempt is not equal to pin, the code will loop
counter -= 1 # Counter decrement by 1
if counter == 0:# break loop if counter is 0
lock_machine() # function to lock the machine if too many wrong guesses
pin_attempt = input('Incorrect, you have ' str(counter) ' more guesses: ')
# Any code that you want to run once the pin is entered correctly goes here
CodePudding user response:
Try this instead:
while pin != 1234:
print('\nWrong pin please try again')
pin = input('\nPlease enter your pin number: ')
showMenu()
CodePudding user response:
To augment the answer by @stuupid, you could create a function to handle inputting until a criteria is met in the general case.
def input_until(prompt, error_msg, predicate, attempts=None, attempts_msg=None):
attempt_no = 1
user_input = input(prompt)
while not predicate(user_input):
attempt_no = 1
if attempts and attempt_no >= attempts:
if attempts_msg:
print(attempts_msg)
return None
print(error_msg)
user_input = input(prompt)
return user_input
At which point, your code can essentially be:
user_input = input_until('Please enter your pin number: ', 'Wrong pin please try again', lambda p: p == '1234')
Or:
user_input = input_until('Please enter your pin number: ', 'Wrong pin please try again', '1234'.__eq__)