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

Why is my variable not getting defined keep getting a "NameError: name 'can_requested' is not defined "

I've stepped through the code and there seems to be an issue with when I am collecting the input from the user, it's not storing it so I can use it in the next section. I am not sure what is missing... here is a screenshot - thanks in advance https://w.trhou.se/ly1hq1rhp4

2 Answers

Steven Parker
Steven Parker
243,253 Points

The code is trying to reference "can_requested" (singular), but the variable created from the input is "cans_requested" (plural).


UPDATE: That is the "correct" system error message, though it's not particularly helpful. To get a more a "friendly" message than the built-in error text, you could check if the exception string contains the standard text and issue your own instead:

    except ValueError as err:
        # check for the "unfriendly" standard message
        if "base 10" in str(err):
            # then do NOT include the standard text in output
            print("Oh no! That's not a valid entry! You must type a number. Do try again...")
        else:
            # Include the error text in the output
            print(f"Oh no, we ran into an issue. {err}. Please try again")

Classic - Thanks Steven - I had check the code about 5 times - even re-wrote it.

My next problem is that I cant seem to get the correct error to display when I pass it an invalid parameter like 'red'

Hey libby, how many cans do you want to purchase? red
Oh no, you ran into an issue. invalid literal for int() with base 10: 'red'. Please try again
There are currently 100 cans remaining!
What is your first name?