0

I have a problem with a program I am making to do with an arithmetic quiz.when I enter a name the quiz should print the welcome message however if I enter a blank or a number then the programme should loop the question but tell the user that they have made an error. I used the try statement and the NameError statement to try and resolve this problem but when I use the function "except" and run it in idle it says that I have an invalid syntax.

here is the code i am trying to fix:

import random 
while True:
    try:
        UserName = input("What is your name:")
        except NameError:
            print ("The answer given does not compute!! Try again")
            continue
        else:
            break

        print(UserName," welcome to the Arithmetic Quiz.")

I have edited the code for the program, but still when I try to run it in Idle it highlights except and then says invalid syntax.

super123
  • 57
  • 5
  • 3
    Your except is on the wrong indentation level – Natecat May 04 '16 at 21:52
  • Can you add the error message? It looks like `except NameError:` is not aligned properly, at least in your example, it should be aligned with the try: statement it corresponds to. – Tom Myddeltyn May 04 '16 at 21:53
  • @Natecat I have moved the indentation to the right place but I still get an error . Thanks – super123 May 04 '16 at 21:54
  • @super123 what error, and update your OP – Natecat May 04 '16 at 21:55
  • @busfault I have aligned the code properly but still not able to get it to work it just says that except is an invalid syntax – super123 May 04 '16 at 21:57
  • What exactly are you trying to prevent in the loop? An empty string? – OneCricketeer May 04 '16 at 21:58
  • @super123 I am not as familiar with Python3, and I assume it is with the `print(...)` format, but can you have a space between `print` and `(...)` edit:"Though, that is not likely the issue since it is after the except. – Tom Myddeltyn May 04 '16 at 21:59
  • Possible duplicate of [Asking user for input until they give valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – OneCricketeer May 04 '16 at 22:00
  • @cricket_007 If the use enters a blank input for the name it should loop the question again. – super123 May 04 '16 at 22:03
  • 1
    @busfault Certainly can. – erip May 04 '16 at 22:03
  • @busfault when I run the code and put a blank input it shows the welcome message and does not loop the question? – super123 May 04 '16 at 22:07
  • @super123 That is because input isn't raising and exception. try running it from the interpreter. does `>>> UserName = input("What is your name:")` give you a NameError exception when you just press enter? or enter a number? this is unless, `input` is some other function that you have defined that raises a NameError? – Tom Myddeltyn May 04 '16 at 22:11
  • 1
    A NameError has nothing to do with whether the input is a valid name of a human being. It indicates that a variable lookup has found no value for the variable. If you're getting such errors out of an `input` call, you're on Python 2 and you should be using `raw_input`. Python 2's `input` is essentially never a good idea. – user2357112 May 04 '16 at 22:15
  • @user2357112 im on python3 – super123 May 04 '16 at 22:17
  • 2
    @super123: Then that line can't raise a NameError. If you want to validate the name entered, you have to check the conditions you're interested in manually. – user2357112 May 04 '16 at 22:18

2 Answers2

2

You don't need a try-except to check for an empty string

while True:
    UserName = input("What is your name:")
    if not UserName:
        print("The answer given does not compute!! Try again")
        continue
    else:
        break
print(UserName," welcome to the Arithmetic Quiz.")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

This is what the proper indentation should be for the try: except: else: block

import random 
while True:
    try:
        UserName = input("What is your name:")
    except NameError: 
        print("The answer given does not compute!! Try again")
        continue
    else:
        break
print(UserName," welcome to the Arithmetic Quiz.")

caveat (thanks cricket_007) this loop will never end. I had meant this comment to show how the try: indentation should have been for the OP.

Tom Myddeltyn
  • 1,307
  • 1
  • 13
  • 27