-3

What I am to do: Write a while loop that prints user_num divided by 2 until user_num is less than 1.

My code:

user_num = 20

while(user_num >= 1):
    print(user_num / 2)
    user_num = float(input())

Error:

10.0 Traceback (most recent call last): File "main.py", line 5, in user_num = float(input()) EOFError: EOF when reading a line

Dmitrii Sidenko
  • 660
  • 6
  • 19
Lia
  • 45
  • 1
  • 1
  • 8

3 Answers3

1

( I am using Python 3.5.2 )

If you do not want to call input:

user_num = 20

while user_num>=1:
    print(user_num/2)
    user_num = user_num/2

Using input (in this case the process stops when the user type a number less than 1):

 user_num = 20

 while user_num>=1:
    print(user_num/2)
    x=float(input())
    user_num = x
lele
  • 119
  • 4
  • please edit your code as such when you post an answer. – Julien Oct 28 '16 at 04:41
  • 1
    also while this might (or not) be the behavior the OP is looking for, it doesn't actually solve the question which is about the error. – Julien Oct 28 '16 at 04:42
  • I don't understand you. She is trying to prints user_num divided by 2 until user_num is less than 1 and I just show the correct way to do it. – lele Oct 28 '16 at 04:47
  • And if the OP ever actually wants to use `input()`? – TigerhawkT3 Oct 28 '16 at 04:52
  • 1
    @leticia The question is not too clear and can be understood as (1) ask the user for number once and divide by 2 and print until less than 1 (I agree it is likely the expected interpretation and that your code solves it), or (2) keep asking user for number until less than one, and if greater than one then divide by 2 and print (the OP's intended code that throws an error). – Julien Oct 28 '16 at 04:52
0

I can reproduce your error messages by doing this (the $ represents the shell prompt):

First create an empty file:

$ >gash.txt 

Redirect stdin from the empty file

$ python gash.py < gash.txt
10.0
Traceback (most recent call last):
  File "gash.py", line 5, in <module>
    user_num = float(input())
EOFError: EOF when reading a line

So, the reason you are getting this error is because the input stream is empty. Hitting EOF on a keyboard (CTRL+D on many systems), does not get the "when reading a line" text.

cdarke
  • 42,728
  • 8
  • 80
  • 84
0
user_num = 20

while user_num >= 1:
    print(user_num/2)

    user_num = user_num/2
Andrea
  • 1