1
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

Jack Black
  • 17
  • 6
  • 1
    your `elif` covers every instance that doesn't meet your `if` condition, so you'll never enter the `elif` – asongtoruin Jun 28 '17 at 10:10
  • 1
    You never reach the `else`. Apart from the two conditions, what else could it be ? :) –  Jun 28 '17 at 10:11
  • The conditions for your `else` to be executed will never happen. If `attempt==pin1` then the `if` block will be run. If `attempt!=pin1` then the `elif` block will be run. If _neither of those happens_ (i.e. never), then the `else` block will be run. – khelwood Jun 28 '17 at 10:11
  • You may find this question helpful: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – PM 2Ring Jun 28 '17 at 10:20

5 Answers5

1

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...'

sachin_hg
  • 136
  • 3
1

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.

asongtoruin
  • 9,794
  • 3
  • 36
  • 47
0

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.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
0

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.

Dziugas
  • 1,500
  • 1
  • 12
  • 28
0

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**

Nuhil Mehdy
  • 2,424
  • 1
  • 21
  • 23
  • Thank you! Just adding more characters to post this as well ;) – Jack Black Jun 28 '17 at 10:18
  • 1
    Except that this code will just ask thrice to enter PIN again, without checking if the entered PIN is correct, then produce the SATAN message. Clearly not what was intended. – Błotosmętek Jun 28 '17 at 10:21