I am using Python 3.4.3. I have successfully written code that uses a try except inside a for loop to ensure my list is being populated with data that I am seeking. The trouble I am having is after an exception is made, when input causes another exception error, it does not repeat the code to reprompt the user for input.
def getTemperatures():
times=["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00",
"08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00",
"16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"]
hourlyTemperatures=[]*len(times)
for x in times:
print(x,"Hours")
while True:
try:
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
except (NameError,SyntaxError,TypeError):
print("\n")
print("You did not input a number.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
while hourlyTemps<-50 or hourlyTemps>130:
hourlyTemperatures.remove(hourlyTemps)
print("That is a temperature outside human habitable environments.")
hourlyTemps=eval(input("""Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:\t"""))
hourlyTemperatures.append(hourlyTemps)
print("\n")
print(hourlyTemperatures)
getTemperatures()
I have tried placing the while loop outside of the for loop, as well as after the try but before the except, and I cannot get it to work.
Here is an example of the output that I get when I test the code:
00:00 Hours
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
You did not input a number.
Please input your temperature in fahrenheit
(round to the nearest degree) for this hour:
Traceback (most recent call last):
File "F:\3.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 11, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\3.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 34, in <module>
getTemperatures()
File "F:\3.4.3 Python Programming\CS117\Labs\Lab03\GetTemperatures.py", line 19, in getTemperatures
(round to the nearest degree) for this hour:\t"""))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
I appreciate any recommendations that anyone may have to offer as to how to fix this problem.