2

I was wondering how I would sum up the numbers they input for n even though it's an input and not int. I am trying to average out all the numbers they input.

n=print("Enter as many numbers you want, one at the time, enter stop to quit. ")
a=0
while n!="stop":
    n=input("Start now ")
    a+=1
user2918356
  • 139
  • 2
  • 3
  • 7
  • 1
    Then shouldn't the question be: "How to read an int from input?" (Of which there are many related questions.) The summing bit would be `a+=n`. – user2864740 Dec 04 '13 at 18:47
  • Not necessarily, this looks like Python 3 (print is a function). – Max Noel Dec 04 '13 at 18:50
  • See http://stackoverflow.com/q/19615427/2864740 (has answers for Python 2 and 3) – user2864740 Dec 04 '13 at 18:50
  • There seems to be confusion if this is about Python 2.x or 3.x; make sure to *add* the appropriate tag. – user2864740 Dec 04 '13 at 18:53
  • @user2864740 I don't think you'd use `a+=n` here; it seems as though poster is using `a` to count the number of inputs (which is better done with a list as suggested below) – thumbtackthief Dec 04 '13 at 22:13

3 Answers3

1

In Python 2.x input() evaluates the input as python code, so in one sense it will return something that you can accept as an integer to start with, but may also cause an error if the user entered something invalid. raw_input() will take the input and return it as a string -> evaluate this as an int and add them together.

http://en.wikibooks.org/wiki/Python_Programming/Input_and_Output

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
jaj2610
  • 19
  • 3
1

You would be better off using a list to store the numbers. The below code is intended for Python v3.x so if you want to use it in Python v2.x, just replace input with raw_input.

print("Enter as many numbers you want, one at the time, enter stop to quit. ")
num = input("Enter number ").lower()
all_nums = list()
while num != "stop":
    try:
        all_nums.append(int(num))
    except:
        if num != "stop":
            print("Please enter stop to quit")
    num = input("Enter number ").lower()

print("Sum of all entered numbers is", sum(all_nums))
print("Avg of all entered numbers is", sum(all_nums)/len(all_nums))

sum & len are built-in methods on lists & do exactly as their name says. The str.lower() method converts a string to lower-case completely.

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
0

Here is one possibility. The point of the try-except block is to make this less breakable. The point of if n != "stop" is to not display the error message if the user entered "stop" (which cannot be cast as an int)

n=print("Enter as many numbers you want, one at the time, enter stop to quit. ")
a=0
while n!="stop":
    n=input("Enter a number: ")
    try:
        a+=int(n)
    except:
        if n != "stop":
            print("I can't make that an integer!")

print("Your sum is", a)
mrKelley
  • 3,365
  • 2
  • 21
  • 28
  • Does the number have to be an integer? What about decimals? – thumbtackthief Dec 04 '13 at 19:29
  • This code will truncate anything to be an integer. So no, the number entered does not have to be an integer, but it will be coerced if possible. – mrKelley Dec 04 '13 at 21:54
  • Sorry--I wasn't clear. I meant that based on the original problem, it seems as though the user would be able to enter decimals, in which case you wouldn't want to convert them to integers. – thumbtackthief Dec 04 '13 at 22:12
  • "how I would sum up the numbers they input for n even though it's an input and not int." - OP. True, this code would be more robust if it could accommodate floats or other number types, but I figured simpler is better since the OP appears to be a beginner. – mrKelley Dec 04 '13 at 22:18
  • True, I'm just unclear on what the OP is looking for and whether int was intentional or he/she didn't know better. – thumbtackthief Dec 04 '13 at 22:28
  • Ask the OP, or provide an answer that handles decimals. – mrKelley Dec 04 '13 at 22:48