0

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
hbss
  • 31
  • 4
  • 5
    You have unbalanced parentheses. – quamrana Sep 21 '19 at 19:25
  • 4
    And broken indentation. – user2357112 Sep 21 '19 at 19:31
  • how do I locate or correct that? Unindent everything and redo it? – hbss Sep 21 '19 at 19:35
  • it's in the `userInputOne` line, but you should be able to locate it by reading the traceback of error messages too. they usually indicate where the error is – Paritosh Singh Sep 21 '19 at 19:38
  • Is this a homework assignment? Why are you being tasked with this with what seems like minimal experience in Python? – Scott Skiles Sep 21 '19 at 19:46
  • I found it all. Also, there was a lot that I found in just the part of the code I shared. Thanks. Resolved and working 100% now – hbss Sep 21 '19 at 19:47
  • Yes it is homework. He allows us to ask for help, but wants us to start trying and ask when you get stumped. Its my 3rd week of class (online). – hbss Sep 21 '19 at 19:48
  • Use an IDE that supports syntax highlighting and linting of Python code. The IDE can usually indicate syntax errors such as incorrect indentation and missing closing parentheses/brackets. – Gino Mempin Sep 22 '19 at 02:20

2 Answers2

2

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.

Eirik Fesker
  • 328
  • 1
  • 12
  • 2
    I will get PyCharm. I have heard of it, but I wasn't sure if it was free or not. – hbss Sep 21 '19 at 19:49
  • There is a free version of PyCharm. I highly recommend it. After a while I actually found benefit in the paid version, too. – Ben Sep 21 '19 at 19:51
0

Your code:

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

To start off with, should look like this:

while True:
    try:
        userInputOne = input(int("How much time in hours a week, do you spend practicing? "))
    except TypeError:
        ...
        ...

Some ways I like to catch and fix this:

  • Run pylint on your file. It will tell you where errors exist and give you warnings for code that can be improved.
  • Use vim and the command == which will attempt to do some auto-indentation.

But the most important thing is to understand that whitespace is important in Python. A syntax error of spacing exists throughout the file. You need to have four spaces rather than the eight in your code. Also, as noted in the comments above, you have some unbalanced parentheses which will break your code. In addition, you have an else statement without an if. Many problems all around, so I'd suggest re-working the code a few lines at a time and make sure that it works before moving forward. In addition, you cannot convert a string to an int, or at the very least you'll get unexpected results.

Scott Skiles
  • 3,647
  • 6
  • 40
  • 64
  • I’m sure that input(int( ... is not correct. – quamrana Sep 21 '19 at 19:44
  • 1
    Good catch! Lots of errors in the code presented. Seems like OP was thrown into this assignment without much experience in Python. – Scott Skiles Sep 21 '19 at 19:45
  • Agreed! Scott S. – quamrana Sep 21 '19 at 19:47
  • Yes. I actually noticed it before realizing you pointed these out. I know it should be int(input( however I'm guessing I just overlooked it due to them both starting with "i". It is 100% working now. Thank you! – hbss Sep 21 '19 at 19:52
  • Pleasure @hbss. Hopefully as you gain more experience with Python these errors stand out more easily. I'll take an upvote if it was helpful as well ;-) – Scott Skiles Sep 21 '19 at 19:54
  • Reputation isn't high enough to show it but I did upvote all answers. Thank you – hbss Sep 21 '19 at 19:57