Posts: 382
Threads: 94
Joined: Mar 2017
May-19-2017, 05:00 PM
(This post was last modified: May-19-2017, 05:00 PM by sylas.)
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
Posts: 8,135
Threads: 159
Joined: Sep 2016
'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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
In addition to @buran's post, I will say that this while loop will never stop. The statement always will be True.
Posts: 382
Threads: 94
Joined: Mar 2017
May-19-2017, 07:02 PM
(This post was last modified: May-19-2017, 07:35 PM by Larz60+.)
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
Posts: 8,135
Threads: 159
Joined: Sep 2016
(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.
Posts: 382
Threads: 94
Joined: Mar 2017
May-19-2017, 08:18 PM
(This post was last modified: May-19-2017, 08:18 PM by sylas.)
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
Posts: 8,135
Threads: 159
Joined: Sep 2016
(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...
Posts: 382
Threads: 94
Joined: Mar 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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Cool, show us what you've got so far :)
Posts: 566
Threads: 10
Joined: Apr 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
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|