NotAGoodCoder
NotAGoodCoder

Reputation: 417

Try/except not excepting ValueError

I have this problem in one of my programs, where the try/excepting of an error to make the program better in case the user accidentally enters something they aren't supposed to, isn't working. It still gives me the error and I'm stumped as to why. Here is the error if it really matters to my issue:

ValueError: invalid literal for int() with base 10: 's'

Here is the snippet of code where I except the error:

    apple_num = int(raw_input(prompt))
    try:
        if apple_num == 3 or apple_num == 2 or apple_num == 1:
            global apples
            apples = apples + apple_num
            time.sleep(0.5)
            pick()
        elif apple_num >= 4:
            print "\nYou can't haul that many apples!"
                time.sleep(0.5)
            pick()
        elif apple_num == 0:
            main()
    except ValueError:
        pick()

Once again, I'm stumped as to why I still get the error, as always, any help is appreciated!

Upvotes: 0

Views: 399

Answers (2)

barak manos
barak manos

Reputation: 30136

Change this:

apple_num = int(raw_input(prompt))
try:

To this:

try:
    apple_num = int(raw_input(prompt))

Upvotes: 3

Daniel
Daniel

Reputation: 42748

The first line is not in the try-except-block.

Upvotes: 4

Related Questions