pin1 = int(input("Please set a pin: "))
print("Welcome to Satan's Soul Bank, Enter ya pin!")
attempt = int(input("Please enter your pin number first"))
if attempt == pin1:
print("Select operation.")
print("1.Deposit Souls")
print("2.Withdraw Souls")
print("3.Check Soul balance")
choice = int(input("Enter choice(1/2/3):"))
elif attempt != pin1:
for i in range(2):
attempt = int(input("Invalid Attempt Please enter your pin
number again"))
else:
print ("Card Swallowed Contact SATAN")
The code itself works apart from the print statement after the else: it seems to not recognize it and just misses it, basically I need it to print that the card has been swallowed after 3 times however when i put it into the elif area it just prints it each time I get the pin wrong so is there any other way to get around that resulting in it printing that the card has been swallowed after the 3rd time
As mentioned in other answers, the elif
block covers the condition not satisfied by if
block. To make sure that you print the statement in else
block, you can make use of a flag
variable which will be set to false
before maximum wrong attempts. Once the maximum attempts is reached, set the flag
to true
.
If the flag
is set to true
, print
'card swallowed. Contact satan...'
You need to restructure your code to get what you want.
pin1 = int(input("Please set a pin: "))
print("Welcome to Satan's Soul Bank, Enter ya pin!")
correct_pin = False
for i in range(3):
attempt = int(input("Please enter your pin number first"))
if attempt == pin1:
correct_pin = True
break
else:
print("Invalid PIN. {} attempts remain.".format(2 - i))
if correct_pin:
print("Select operation.")
print("1.Deposit Souls")
print("2.Withdraw Souls")
print("3.Check Soul balance")
choice = int(input("Enter choice(1/2/3):"))
else:
print ("Card Swallowed Contact SATAN")
We loop 3 times, exiting if the user gets the correct pin, and only offer further options if the pin was correct.
In your code you have
if attempt == pin1:
...
elif attempt != pin1:
...
Since one of the conditions always holds (either attempt
equals pin1
or not), the program will never arrive to the else
part.
The 'else' block in your code only runs if the if
and the elif
blocks are both false. The only way this could happen is if (attempt == pin1)
is false and (attempt != pin1)
is false.
As you can see this is impossible because either they are equal or they're not - it can't be neither. This is why print ("Card Swallowed Contact SATAN")
will never run.
It seems, you wanted the program to behave some thing like the following:
pin1 = int(input("Please set a pin: "))
print("Welcome to Satan's Soul Bank, Enter ya pin!")
attempt = int(input("Please enter your pin number first"))
if attempt == pin1:
print("Select operation.")
print("1.Deposit Souls")
print("2.Withdraw Souls")
print("3.Check Soul balance")
choice = int(input("Enter choice(1/2/3):"))
elif attempt != pin1:
for i in range(2):
attempt = int(input("Invalid Attempt Please enter your pin number again"))
print ("Card Swallowed Contact SATAN")
After two incorrect attempt, you wanted to tell the user to contact SA**
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.