Ask coding questions

← Back to all posts
How to make a python program that asks how much numbers you want to add, and then adds it?
h
adityavengata (18)

I want a python program that asks for how much numbers the user wants to add, then asks them for the numbers, and then prints the result.

ALSO please explain since im trying to learn python

Answered by xfinnbar (149) [earned 5 cycles]
View Answer
Comments
hotnewtop
xfinnbar (149)
amount = input("How many numbers would you like to add?")

numbers = [] # list to store numbers
for i in range(int(amount)): # execute this code amount times, with i as the amount of times run.
  numbers.append(input(f"Enter number {i + 1}..."))

result = 0 # we will add to this
for number in numbers: # for each number in the list of numbers, add it to result
  result += float(number) # += means add and assign (eg. num = num + 1 is the same as num += 1)

print(f"Answer: " + str(result))
Aivoybia (26)
addnumvar = 0
numofinput = int(input("How many numbers do you want to input?")
for i in range(numofinput):
  addnum = int(input("Enter a number")
  addnumvar += addnum

What I did was create a variable to add the numbers
Then I made an input to see how many numbers the user wants to add
Then I made a for loop and the range is the number the user input for the step above
Then I made another integer input and added it to addnumvar

Hope this helps, and have an awesome day!!! :D

Much love,

@Aivoybia

adityavengata (18)

@Aivoybia It gives me a syntax error for some reason