Search code examples
pythonsyntaxexcept

try:/except: returns a syntax error in Python when trying to run module


Below is my code and the error is on line 4 "except" gives a syntax error... (this is a debugging and error catching assignment. I don't see what is wrong with it)

while True:
        try:
                userInputOne = input(int("How much time in hours a week, do you spend practicing? ")
        except TypeError:
                print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
                break
    else:
        userInputTwo = str(input"How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
        if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
            print("Please use one of the options. ")
        else:
            print("Let's calculate...")
            break

Solution

  • I Attached the working code. Syntax Error was caused by missing parethesis and wrong indents. Take a look at your else: statement. It's not at the same height as the try: statement. TypeError means, that you dont have to convert your input to strings, because they already are. Otherwise i suggest you create some variables and convert them via int() when you want to calculate with them.

    while True:
        try:
            userInputOne = input("How much time in hours a week, do you spend practicing? ")
        except TypeError:
            print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
            break
        else:
            userInputTwo = input("How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
            if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
                print("Please use one of the options. ")
            else:
                print("Let's calculate...")
                break
    

    Edit: I recommend using PyCharm (if you don't) with its Auto-Indent function and nice "indent guidelines". So you can see many mistakes much easier.