Posts: 382
Threads: 94
Joined: Mar 2017
Apr-15-2017, 01:50 PM
(This post was last modified: Apr-15-2017, 01:59 PM by Larz60+.)
Hi all . The folllowing file does not stop on the presence of CR. I wish i finish inputs on the presence of CR, that is nothing entered. At the presence of CR I should like to get the sum of all and the average value.
my code here
# file4.py
import sys
def getvalue():
value=input("enter the value ")
print("value entered : ", value)
#if (value="")
# sys.exit(0)
return value
size=40
print("here is the addition of many numers (even with a .) Maximum of numbers=",size)
print("once you have finished your list of numbers, just press ENTER to indicate the end\n")
sum=0.0
value=0.0
count=1
print("bonjour")
while (count != 0):
getvalue()
sum += value
++count
Posts: 566
Threads: 10
Joined: Apr 2017
Since you commented out the part that exits on empty value..... what do you expect?
And, if you want infinite loop - the proper construct will be
while True: And if you exit - what happens to sum.
Your code just don't make sense
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.
Posts: 382
Threads: 94
Joined: Mar 2017
This program works very well with C++. I think it should be the same with python3. I modified a little my code Here thenew
# file4.py
import sys
def getvalue(value, sum):
value=input("enter the value ")
print("value entered : ", value)
#if input=CR then return
sum += value#put value into sum, (apparently correct for only integers)
print("sum", sum)
return ;#value;
size=40
print("here is the addition of many numers (even with a .) Maximum of numbers=",size)
print("once you have finished your list of numbers, just press ENTER to indicate the end\n")
sum=0.0
value=0.0
count=1
while (count != 0):
getvalue(0,0)
count=count +1
Posts: 566
Threads: 10
Joined: Apr 2017
You have commented out your exit line
if not value.strip():
exit(0) or
if value.strip() == '':
exit(0) strip will remove all space variants - including CR - at input borders. I think that in Python3 it's unnecessary, but to be on the safe side...
And
while True:
get_value(0.0) (You don't need counter, and in Python you separate words in identifiers by underscores)
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.
Posts: 382
Threads: 94
Joined: Mar 2017
On the presence of a CR, I should like to return and then give the total sum and the average value. Why with C++ sum+=value worlks even with doubles??
Posts: 566
Threads: 10
Joined: Apr 2017
I don't usually work with user input, but obviously - it's logical, but I've still checked it - it returns string value. Convert it to float.
Python - unlike C++ - (cursed be  , just kidding) - allows implicit conversion. And adding for string is concatenation. It would have helped if you have shown the output you don't like.
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.
Posts: 382
Threads: 94
Joined: Mar 2017
Here is what I get now:
sum += value
TypeError: unsupported operand type(s) for +=: 'float' and 'str'
Posts: 2,953
Threads: 48
Joined: Sep 2016
Hello!
You want to get a bunch of real numbers from the input ( separated with spaces ? ) and print out the sum and the average?
Posts: 382
Threads: 94
Joined: Mar 2017
If thiis is easier, why not ? Thank you very much
Posts: 2,953
Threads: 48
Joined: Sep 2016
In [1]: nums = input("Write your numbers: ")
...:
Write your numbers: 3 14 5.43 723.98 65
In [2]: nums_l = [float(n) for n in nums.split()]
In [3]: nums_l
Out[3]: [3.0, 14.0, 5.43, 723.98, 65.0]
In [4]: print("Numbers: {}\nSum: {}\nAvarage: {:.3f}".format(nums_l, sum(nums_l), sum(nums_l) / len(nums_l)))
Numbers: [3.0, 14.0, 5.43, 723.98, 65.0]
Sum: 811.41
Avarage: 162.282
|