How to get a list of numbers as input and calculate the sum?

后端 未结 4 949
有刺的猬
有刺的猬 2021-01-23 05:34
mylist = int(raw_input(\'Enter your list: \'))    
total = 0    
for number in mylist:    
    total += number    
print \"The sum of the numbers is:\", total

相关标签:
4条回答
  • 2021-01-23 06:13

    It appears you are attempting to convert a string of numbers that most likely looks like this(you never specified input format):

    "1 23 4 45 4"
    

    OR this

    "1, 45, 65, 77"
    

    to an int. This naturally won't work as only one number at a time could be converted like this. For instance int('45') will work, but int('12 34, 56') will not.

    I think what you need to do is something like this:

    mylist = raw_input("Enter a list of numbers, SEPERATED by WHITE SPACE(3 5 66 etc.): ")
    # now you can use the split method of strings to get a list
    mylist = mylist.split() # splits on white space by default
    # to split on commas -> mylist.split(",")
    
    # mylist will now look something like. A list of strings.
    ['1', '44', '56', '2'] # depending on input of course
    
    # so now you can do
    total = sum(int(i) for i in mylist)
    # converting each string to an int individually while summing as you go
    

    This isn't the most compact answer, but I think it breaks it down for you to better understand. Compactness can come later.

    0 讨论(0)
  • 2021-01-23 06:18

    if you want to store numbers in list, create list. also add a loop for user enters values. you can assign an input to break the loop or add a counter.

    myList = []
    counter = 0 
    while counter<10: #change this to true for infinite (unlimited) loop. also remove counter variables
        num = raw_input("Enter numbers or (q)uit")
        if num == 'q': 
            break
        else:
            myList.append(int(num))
        counter +=1
    
    print sum(myList)  
    

    or without list

    >>> while True:
    ...     num = raw_input("number or (q)uit")
    ...     if num == 'q': break
    ...     else:
    ...             total +=int(num)
    ... 
    

    result

    number or (q)uit4
    number or (q)uit5
    number or (q)uit6
    number or (q)uit7
    number or (q)uit8
    number or (q)uit9
    number or (q)uit9
    number or (q)uitq
    >>> total
    48
    
    0 讨论(0)
  • 2021-01-23 06:24

    You are not creating a list. You are creating a string with your user input and attempting to convert that string to an int.

    You can do something like this:

    mylist = raw_input('Enter your list: ')
    mylist = [int(x) for x in mylist.split(',')]
    total = 0    
    for number in mylist:    
        total += number    
    print "The sum of the numbers is:", total
    

    Output:

    Enter your list:  1,2,3,4,5
    The sum of the numbers is: 15
    

    What did I do?

    I changed your first line into:

    mylist = raw_input('Enter your list: ')
    mylist = [int(x) for x in mylist.split(',')]
    

    This accepts the user's input as a comma separated string. It then converts every element in that string into an int. It does this by using split on the string at each comma.

    This method will fail if the user inputs a non-integer or they don't comma separate the input.

    0 讨论(0)
  • 2021-01-23 06:35

    Correct way of doing the same is:

    separator = " " #Define the separator by which you are separating the input integers.
    # 1,2,3,4    => separator = ","
    # 1, 2, 3, 4 => separator = ", "
    # 1 2 3 4    => separator = " "
    
    mylist = map(int, raw_input("Enter your list : ").split(separator)) 
    
    print "The sum of numbers is: "+str(sum(mylist))
    

    To find the sum you need to convert the space separated characters into int individually as done above using map function.

    0 讨论(0)
提交回复
热议问题