0

Hey does anyone know what error handling I would need to use using the Except command? Thanks and this is the code I am trying to put it to.

try:
    #Get average, amount earned
    school_average = input("Enter your average in school:")
    money_earned = input("How much money did you earn before summer: ")

#determine the resulting vacation
if school_average >= 80 and money_earned >= 500:
    print "You get to go to Europe!"
elif school_average >= 80:
    print "You get to go to California."
else:
    print "You do not get to go away."
except NameError:
    print "Please input a number for your average and amount of money earned."
except:
    print "An error has occurred"
Shapi
  • 5,493
  • 4
  • 28
  • 39
poop
  • 3
  • 1
  • 2
  • 5

2 Answers2

1

You can manually throw an error with raise which will be caught be a general except statement (with no exception type specified).

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
1

You can implement it something like this:

n = int(input("What is it?\n"))

if 100 > n and n > 0:
    print("you did good")
else:
    raise ValueError('it must be between 0 and 100')

If the answer is above 100 or less than 0, it will raise the ValueError execption as such:

What is it?
101
Traceback (most recent call last):
  File "C:/Users/Leb/Desktop/Python/test2.py", line 6, in <module>
    raise ValueError('it must be between 0 and 100')
ValueError: it must be between 0 and 100

For more detailed information check out this answer: Manually raising (throwing) an exception in Python

Community
  • 1
  • 1
Leb
  • 15,483
  • 10
  • 56
  • 75