Python Forum
How to stop inputs on the presence of CR
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to stop inputs on the presence of CR
#1
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
Reply
#2
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.
Reply
#3
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
        
Reply
#4
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.
Reply
#5
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??
Reply
#6
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.
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.
Reply
#7
Here is what I get now:
sum += value
TypeError: unsupported operand type(s) for +=: 'float' and 'str'
Reply
#8
Hello!
You want to get a bunch of real numbers from the input ( separated with spaces ? ) and print out the sum and the average?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
If thiis is easier, why not ? Thank you very much
Reply
#10
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking the presence of label using pywinauto module Malt 0 1,927 Jul-26-2019, 09:06 AM
Last Post: Malt
  [split] Check presence of a file pascale 25 10,995 Jul-08-2018, 12:23 PM
Last Post: pascale
  Check presence of a file Steffenwolt 8 5,747 May-25-2018, 07:04 AM
Last Post: Steffenwolt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020