Dier Eluom
asked on
Error catching in Python
def weather():
import sys
raining=input('Is it raining? ')
while True:
try:
x =(input("Please enter 'yes' or 'no': ")
except ValueError:
print("That was not a valid answer. Try again...")
if raining == 'yes':
ask =(input('Do you have an umbrella? '))
if raining=='no':
print('Go outside ')
sys.exit(0)
if ask == 'yes':
print('Go outside ')
sys.exit(0)
while raining == 'yes': # this will ensure, that you do not enter the loop if it's not raining
print('Wait until it stops.')
still=input('Is it still raining? ')
if still =='no':
break
print('Go outside !')
weather()
I want to be able to handle incorrect responses - user should add either yes or no. Having trouble with the syntax.
def weather():
import sys
raining=input('Is it raining? ')
while True:
try:
x =(input("Please enter 'yes' or 'no': ")
except ValueError:
print("That was not a valid answer. Try again...")
if raining == 'yes':
ask =(input('Do you have an umbrella? '))
if raining=='no':
print('Go outside 1')
sys.exit(0)
if ask == 'yes':
print('Go outside 2')
sys.exit(0)
while raining == 'yes': # this will ensure, that you do not enter the loop if it's not raining
print('Wait until it stops.')
still=input('Is it still raining? ')
if still =='no':
break
print('Go outside 3!')
weather()
ASKER
What is "m?
ASKER
Not sure what you mean by that. Just throws error.
oops typo and forgot to change 'input' to 'prompt' (I got interrupted while writing your response and I pressed 'send' too early)
it should have been a "," .
('m' is next to ',' on a French keyboard)
it should have been a "," .
('m' is next to ',' on a French keyboard)
x =prompt("Please enter 'yes' or 'no': ", ["yes", "no"])
ASKER
Is it raining? hjjh
Traceback (most recent call last):
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 9, in weather
x=prompt("Please enter 'yes' or 'no'':",["yes","no"])
NameError: name 'prompt' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 32, in <module>
weather()
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 10, in weather
except valueError:
NameError: name 'valueError' is not defined
Traceback (most recent call last):
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 9, in weather
x=prompt("Please enter 'yes' or 'no'':",["yes","no"])
NameError: name 'prompt' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 32, in <module>
weather()
File "C:\me\My Documents\Python 3.5\WeatherNewError.py", line 10, in weather
except valueError:
NameError: name 'valueError' is not defined
ASKER
I am using Python 3.5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome, thank you!
try: / except: Doesn't really help you here as another string than yes or now will not provoke an exception.
It would be useful for places where you try to convert a string to an integer. A ValueError would occur if you passed a srtring containing something else than digits.
Example:
Open in new window
No just replace
Open in new window
withOpen in new window