Question

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!

Was it helpful?

Solution

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

OTHER TIPS

Change this:

apple_num = int(raw_input(prompt))
try:

To this:

try:
    apple_num = int(raw_input(prompt))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top