Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

If statement is output along with exception

Hello All,

I am working on the guessing game. In my code, if I choose to put in a noninteger number instead of a string, I want the exception I allow for to show. It does, but the last part of my conditional statements is also showing too. I know it has to be a simple fix can anyone help?

My code is linked: https://w.trhou.se/vd12wacxjm

2 Answers

Steven Parker
Steven Parker
231,261 Points

It's normal for the code after the except block to also be executed. If you don't want it to be, you can use a continue statement to cause the loop to start over immediately:

    except ValueError:
        print("That is not a number. Please try again")
        continue    # start loop over without reporting on the number
Steven Parker
Steven Parker
231,261 Points

Maximilian Hill — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

Steven Parker Thank you!