A. Spoon
A. Spoon

Reputation: 21

Appending a list with iterating user input then adding list index's together

NumberOfNumbers = int(input("CHoose up to 5 numbers to add together"))
NumbersToBeAdded = []


while len(NumbersToBeAdded) < NumberOfNumbers:
    Number = input("What number would you like to add?")
    NumbersToBeAdded.append(Number)
total = NumbersToBeAdded[0] + NumbersToBeAdded[1] + NumbersToBeAdded[2] + NumbersToBeAdded[3] + NumbersToBeAdded[4]
print(total)

Im attempting to have a user input how many numbers they want to add together, then ask them for the numbers they want to add together - EG 4 numbers inputted would loop 4 inputs and add them to the list (NumbersToBeAdded). The list would then take each index value and add them all together.

A few problems: 1. inputing 10 five times results in 1010101010 rather than 50 2. Im having trouble figuring out the scalability - id rather have the user not be restricted to an X number of inputs, so they could input 3 numbers or 999 if they wished, but how could i loop the adding of all index values together - for example, it iterates through the list adding all the values up and the number of times it iterates would be decided by NumberOfNumbers rather than doing index0 + index1 + index2 etc? obviously thats very impractical and inefficient and doesnt scale beyond the number of index values i program to be added. 3. Kind of not related to this but i ran into while trying to figure this out.. Im not sure what the X in "while X in Variable" does.

Upvotes: 2

Views: 2786

Answers (4)

gil
gil

Reputation: 2144

input returns a string. That's why you get "1010101010", which is five "10"s concatenated, instead of the sum of 5 10s. Convert to integer using int, as you did in your first line.

Also a list is not really necessary. You can just keep a running sum instead, like so:

number_of_numbers = int(input("How many numbers do you want to type in? "))

total = 0
for _ in range(number_of_numbers):
    total += int(input("What number would you like to add? "))
print(total)

If you want to let your user enter a fixed number of numbers, a for loop is appropriate (as shown above). while is good for letting user enter an arbitrary number of numbers until a certain input (say, a non-number). The program below lets the user keep entering integers until he enters something that can't be converted to an integer, upon which it prints out the sum:

total = 0
while True:
    try:
        total += int(input('Enter a number to be added > '))
    except ValueError:
        # when the input cannot be converted to a int
        break
print(total)

If you are not familiar with try-except, you can learn about them.

Upvotes: 0

Filippo Costa
Filippo Costa

Reputation: 498

The built-in sum comes very handy in these situations:

# First tip: you're not using a good naming convention. Do not use PascalCase for variables
how_many = int(input("Choose up to 5 numbers to add together:"))
num = []

# Much cleaner and readable, isn't it?
for _ in range(how_many):
    num.append(int(input("What number would you like to add?")))

print('The sum is: {}'.format(sum(num)))

Upvotes: 0

albert
albert

Reputation: 8593

You can make your code more versatile using a for-loop in combination with range() instead of a while-loop. In addition a list of numbers can be summed up by using sum().

So improving the code and casting all inputs as integers, I suggest the following:

NumberOfNumbers = int(input("How many numbers do you want to type in? "))
NumbersToBeAdded = []

for i in range(0, NumberOfNumbers):
    Number = int(input("What number would you like to add? "))
    NumbersToBeAdded.append(Number)

total = sum(NumbersToBeAdded)

print('The sum is: {}'.format(total))

However, you should think about what happens if a user enters something which cannot be casted to an integer hence this would raise an exception, currently. For this I suggest having a look at so called try-except-blocks in order to cast the input and handle an exception if it would fail.

Upvotes: 0

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

As for your first issue, you need to convert input values to integer just before adding them into the list:

NumbersToBeAdded.append(int(Number))

Second, you can use sum to sum all numbers added into the list instead of writing sth like NumbersToBeAdded[0] + NumbersToBeAdded[1] + ...:

total = sum(NumbersToBeAdded)

Upvotes: 1

Related Questions