Reputation: 11
I'm having trouble with writing a program that asks for the number of days in a month, only accepting values between 28 and 31 inclusive; then continuing to ask until it receives an acceptable value. Then asks for the daily high temperature (in Fahrenheit degrees) for each day. Only accepting values between -50 and 110; continuing to ask for values for each day until it receives an acceptable value.
This is what I have so far. I'm getting a TokenError: EOF in multi-line statement on line 11
error. I'm stuck on how to prompt the user to enter the high temperature of each day in the range--the last line of what I have.
totalnumberofday=0
totalDayHighTemp=0
num = int(input("Enter the number of days in a month:"))
if num <=28 or num>=31:
num=int(input("please reenter an acceptable value:"))
for i in range(1, num+1):
DayHighTemp=float(input("Please enter the daily high temperature"+ format(i+"d"))
Upvotes: 1
Views: 47
Reputation: 557
There's a few problems, let's go through them one-by-one to show how to solve it:
File "a.py", line 12
^
SyntaxError: unexpected EOF while parsing
A SyntaxError means that there's something wrong with the syntax, or grammar, of the script. The message tells us it reached End Of File while it was expecting more code. Looking at the code, there's a missing )
at the last line to close your DayHighTemp=float(...
statement. Adding the )
solves this issue.
Running the file again, we get this issue:
Enter the number of days in a month:30
Traceback (most recent call last):
File "a.py", line 10, in <module>
DayHighTemp=float(input("Please enter the daily high temperature"+ format(i+"d")))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Here, it says that we can't add an integer and string together. It looks like format(i+"d")
was meant to be format(i,"d")
instead.
Now, we can finally get to your core problem: "continuing to ask until it receives an acceptable value."
Your solution for asking the valid day is to check it only once, but then not to check if you enter it in a second time:
num = int(input("Enter the number of days in a month:"))
if num <=28 or num>=31:
num=int(input("please reenter an acceptable value:"))
Instead, it would be better to keep asking the question until it receives a valid response -- or, phrased better, to keep asking while the response is invalid.
While there's multiple ways of doing this, here's one way to do it:
num = int(input("Enter the number of days in a month:"))
while num <=28 or num>=31:
num=int(input("please reenter an acceptable value:"))
I'll leave it as an exercise for you to adapt this towards ensuring a valid temperature.
Upvotes: 2