Python Forum
instruction refused on a "while loop" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: instruction refused on a "while loop" (/thread-3392.html)

Pages: 1 2


instruction refused on a "while loop" - sylas - May-19-2017

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



RE: instruction refused on a "while loop" - buran - May-19-2017

'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.


RE: instruction refused on a "while loop" - wavic - May-19-2017

In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.


RE: instruction refused on a "while loop" - sylas - May-19-2017

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


RE: instruction refused on a "while loop" - buran - May-19-2017

(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.



RE: instruction refused on a "while loop" - sylas - May-19-2017

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



RE: instruction refused on a "while loop" - buran - May-19-2017

(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...


RE: instruction refused on a "while loop" - sylas - May-19-2017

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.


RE: instruction refused on a "while loop" - nilamo - May-19-2017

Cool, show us what you've got so far :)


RE: instruction refused on a "while loop" - volcano63 - May-19-2017

(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