I was given a problem to solve, it was like this:
Write a program that asks the user to input numbers and to end the program type -1, after return the sum of the numbers inputted.
This is what I've done:
num=0
i = 0
while num != -1: #loop to get the numbers
i = i num #this will make the sum of the inputted numbers
num = int(input("Input a number: ")) #save the input given into "num"
print(num) #this shows the number that has been inputted by the user
print("Input -1 to terminate")
print(i) #this shows the total sum of all the inputted numbers
It does what it was asked, but now I want on my last print() to give the numbers the user inputted, not the sum of them all. How can I do that?
CodePudding user response:
You can store the user's numbers in a Python list: https://www.w3schools.com/python/python_lists.asp
You can create a new list by doing something like numbers = []
, which will create an empty Python list. Then, on each iteration in your while loop, you can add the number to this list by doing numbers.append(i)
, which will add the new number to the end of this list. Once you are ready to display all the numbers, you can use a for loop to display all of the numbers that are held in the list: for num in numbers: print(num)