+ 1
How do you stop getting EOFErrors when getting user input?
It seema that every single time I try and get user input I always end up getting EOFErrors. Here is my code and Error. Error: Traceback (most recent call last): File "/usercode/file0.py", line 3, in <module> weight = int(input()) EOFError: EOF when reading a line Code: while True: weight = int(input()) height = float(input()) formula = int(weight) / height**2 if formula < 18.5: print("Underweight") elif formula >= 18.5: if formula < 25: print("Normal") elif formula >= 25: if formula < 30: print("Overweight") elif formula >= 30: print("Obesity")
8 Answers
+ 1
I recognize this as the BMI Code Coach task.
You need not loop your code. Code Coach will run it from the beginning once for each test.
+ 2
Would you like a friendly tip?
There are logic errors in the program. The first if statement is fine. After that, testing for
>=18.5 is unnecessary and it gets you into trouble.
if formula < 18.5:
print("Underweight")
elif formula >= 18.5:
if formula < 25:
print("Normal")
Suppose formula is 30. Carefully walk through the logic and you should find there will be no output. Here is the pattern to follow for a correction:
if formula < 18.5:
print("Underweight")
elif formula < 25:
print("Normal")
Apply the pattern to the remaining logic, too.
+ 1
Normally you can prevent EOF errors by supplying enough input. However in this case it would be impossible because the code starts with while True, which is an infinite loop. It will never stop prompting unless it encounters an exception. EOF is that exception.
0
I need to be able to have the input asked as many times as the user likes. So if it is impossible to provide this by using an infinite loop, how should I go about doing it?
0
hi you could use a try and except statement.
Like this:
try:
#your code
except EOFError as e:
#your code
Good luck đ
0
A relevant and timely post:
https://www.sololearn.com/post/1332896/?ref=app
đđđ
0
You can try a break statement, which will break out of the infinite loop when the user is done entering input. For example:
While True:
txt = input(âEnter your height, type done to exitâ)
if txt == âdoneâ:
break
#continue your program if user didnât enter âdoneâ
txt = int(txt)
weight = float(input())
...more code...