Frage

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!

War es hilfreich?

Lösung

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

Andere Tipps

Change this:

apple_num = int(raw_input(prompt))
try:

To this:

try:
    apple_num = int(raw_input(prompt))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top