0

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.

1 Answer 1

0

Use the else with the try statement to ensure that the following code will only get executed if no exception occurs. Also your code has no way to exit the while loop so I'd suggest adding break where you need to exit the while loop and continue with the for. Also, the while that checks whether the temperature is within boundaries is pretty much useless the way you put it. In your code, the temp gets appended to the list and then the check runs. So instead do smth like this:

    while True:
        try:
            hourlyTemps = float(input("""Please input your temperature in fahrenheit
            (round to the nearest degree) for this hour:\t""")) # the eval is unneccesary, the number check is simpler
            if -50 < hourlyTemps < 130:
                hourlyTemperatures.append(hourlyTemps)
                break
        except (TypeError, ValueError):
            print() # putting the \n char is also unneccesary
            print("You did not input a number.")
        print()

It's as simple as that. Please, ask questions if something isn't clear. Good luck!

1
  • Thank you for your input. It turns out that I do not need the if statement, and keeping it as my while loop, but outside the first while loop works just as well. The rest of my code is now similar to what you've suggested, so thank you for your help!!! Commented Mar 9, 2016 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.