Python Forum

Full Version: instruction refused on a "while loop"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
my question here:Why the following tiny  file of 4 lines works well? 1. value =5.5...    2.total=0.0....3. total=value+total....4...print(total)....This works well....but the following code does not work

# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
count = 1
while (count != 0):
   value = input("enter the value: ")
   print(value)
   print(total)
   total=value+total#not accepted. <interpreter speaks of "module"
   print(total)
   # if value.strip == '':#stop inputs on CR
   average = total / count
   print("average value= ", average)
   count = count + 1
'not working' is not very informative. Please, in the future post full Traceback in error tags. Usually the traceback is pretty informative what the error is where it is.
In this particular case the problem is that input return string (i.e. type str). So you need to convert the user input to numbers using built-in functions float(), int() before you can use in calculations.
In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.
HERE is more information given by the output........................................................................................................................enter the value: 4.8
4.8
0.0

Error:
Traceback (most recent call last):  File "/home/sylvain/PycharmProjects/py2/file5.py", line 12, in <module>    total=value+total#not accepted. <interpreter speaks of "module" TypeError: Can't convert 'float' object to str implicitly
Process finished with exit code 1
(May-19-2017, 05:36 PM)buran Wrote: [ -> ]In this particular case the problem is that input return string (i.e. type str). So you need to convert the user input to numbers using built-in functions float(), int() before you can use in calculations.
My first error disappeared.  I did correct it.The problem now is that it is impossible to stop inputs on a CR. Have you an idea of a character to use in order to stop inputs. Here is my new code : ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
    value = float(input("enter the value: "))
    print(value)
    print(total)
    total = value + total  
    print(total)
    if value == '':  # stop inputs on CR or other character
        average = total / count
        print("average value= ", average)
        count = count + 1
(May-19-2017, 06:15 PM)wavic Wrote: [ -> ]In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.
well, it's up to you - when do you want it to stop? i.e. until user enter something specific, when he count reach certain value, etc...
I wish to stop inputs on a specific character or combination of 2 characters ( use of &&) for example. I am a beginner in python so I am unable to finish that. Thanks for your cooperation.
Cool, show us what you've got so far :)
(May-19-2017, 08:18 PM)sylas Wrote: [ -> ]My first error disappeared.  I did correct it.The problem now is that it is impossible to stop inputs on a CR. Have you an idea of a character to use in order to stop inputs. Here is my new code : ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# file5.py
size = 40
print("here is the addition of many numbers (even with a .) Maximum of numbers=", size)
print("Once you have finished your list of numbers, just press ENTER to indicate the end\n")
total = 0.0
value = 0.0
count = 1
while (count != 0):
    value = float(input("enter the value: "))
    print(value)
    print(total)
    total = value + total  
    print(total)
    if value == '':  # stop inputs on CR or other character
        average = total / count
        print("average value= ", average)
        count = count + 1

Your immediate conversion to float will cause exception if you just press CR when prompted
>>> value = float(input())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
>>>
You will leave the loop - without getting the results
Pages: 1 2