Home > Net >  How to add X amount number from user input then add all of number in python?
How to add X amount number from user input then add all of number in python?

Time:12-25

how_many_number = int(input("How many number do you want to print? "))

for take_number in how_many_number:

take_number = int(input("Enter number: "))
sum = 0
sum = sum   take_number

print(sum)

CodePudding user response:

Here you go. To take user input we can use a for loop and for each iteration we can add it to our sum using = operator. You can read through the following code to understand it well enough.

number_of_inputs = int(input("How many number to sum: ")

sum = 0  # Initialize the sum variable
for x in range(number_of_inputs):  # Repeat the number of inputs times
    sum  = int(input("Enter Value: "))  # Take a input and add it to the sum

print("Sum is", sum)  # print out the sum after completing

CodePudding user response:

You can also compress it into a List Comprehension, like this...

how_many_number = int(input("How many number do you want to print? "))
print(sum([int(input("Enter number: ")) for i in range(how_many_number)]))
  • Related