문제

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!

도움이 되었습니까?

해결책

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

다른 팁

Change this:

apple_num = int(raw_input(prompt))
try:

To this:

try:
    apple_num = int(raw_input(prompt))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top