-3

I have a very simple try/except block to basically force the variable 'password' to be defined as an integer from user input.

It is likely a dumb question, but I have tried looking around and cannot find some solution.

    try:
        password = int(input('Password: '))

    except ValueError:
        print("Please do not type letters or symbols!!")

    while type(password) != 'int':
            try:
                password = int(input('Password '))
            except ValueError:
                print("Please do not type letters or symbols!!")

    print('Complete, we have your password.')

But, when I try run this python shell, it comes up with a Syntax Error, highlighting 'except'...

Bonzo
  • 427
  • 2
  • 10
  • 28
Turdle
  • 3
  • 3
  • 1
    The extra new line after the `password = ...` line will cause this issue in the python shell. Try running it as a file and it should work. – rdas Oct 14 '19 at 06:19
  • 3
    Also count your parentheses and that's not how you check if a value is an integer. – jonrsharpe Oct 14 '19 at 06:21
  • Worth reading: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Oct 14 '19 at 08:36

2 Answers2

0

Remove the new line after password = .... Shell expects an except block not a new line. Also your parenthesis are not complete.

try:
    password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))
except ValueError:
    print("Please do not type letters or symbols!!")
Kshitij Saxena
  • 930
  • 8
  • 19
0

There were couple of problems:

  1. You have to use the comparision like this type(password) is not int:
  2. Define password variable beforehand, otherwise it would not be recognized in your while statement
    password=""
    try:
        password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))


    except ValueError:
        print("Please do not type letters or symbols!!")

    while type(password) is not int:
        try:
            password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))

        except ValueError:
            print("Please do not type letters or symbols!!")

    print('Complete, we have your password.')
san
  • 1,415
  • 8
  • 13
  • Sure, I'll work on that bit later. RIght now it won't even run because the Syntax Error popup. Is the try/except statement formatted correctly? – Turdle Oct 14 '19 at 06:31
  • Copy and paste this code in an IDE (eg. Pycharm), execute the program. Please let me know if you still see the issue. – san Oct 14 '19 at 06:35
  • Beaut. Worked on both default and Pycharm. I think I should have had the varaible introduced above...? Anyway, thankyou for the help that has solved it. I am quite new to this so everything has been appreciated. – Turdle Oct 14 '19 at 06:38
  • @Turdle you are welcome. If this post helped, please feel free to accept the solution and vote it :-) – san Oct 14 '19 at 06:39