+ 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")

18th Oct 2021, 11:14 PM
WebFreak
WebFreak - avatar
8 RĂ©ponses
+ 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.
18th Oct 2021, 11:27 PM
Brian
Brian - avatar
+ 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.
18th Oct 2021, 11:43 PM
Brian
Brian - avatar
+ 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.
18th Oct 2021, 11:21 PM
Brian
Brian - avatar
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?
18th Oct 2021, 11:24 PM
WebFreak
WebFreak - avatar
0
hi you could use a try and except statement. Like this: try: #your code except EOFError as e: #your code Good luck 😊
19th Oct 2021, 12:41 AM
Stefanoo
Stefanoo - avatar
0
A relevant and timely post: https://www.sololearn.com/post/1332896/?ref=app 😄😄😄
19th Oct 2021, 3:02 AM
Brian
Brian - avatar
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...
20th Oct 2021, 10:21 AM
Patrick M