Python Forum

Full Version: How to stop inputs on the presence of CR
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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
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
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
        
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)
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??
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 Angry, 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.
Here is what I get now:
sum += value
TypeError: unsupported operand type(s) for +=: 'float' and 'str'
Hello!
You want to get a bunch of real numbers from the input ( separated with spaces ? ) and print out the sum and the average?
If thiis is easier, why not ? Thank you very much
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
Pages: 1 2 3