182k views
0 votes
Use a while loop to repeatedly take input from the user and calculate a sum. The user will only be allowed to type in numbers, but remember the input function returns strings (so you will need to convert them with the built-in int function). If the user inputs the value -999, then the program should stop and print out the sum of all the numbers entered.

User Asicfr
by
5.0k points

1 Answer

4 votes

Answer and Explanation:

#take input from user

n = int(input("Enter any number: "))

#sum variable to store sum

sum=0

#condition to exit from loop

while n!=-999:

#adding entered number in sum variable

sum=sum+n

#take input from user

n = int(input("Enter any number : "))

#print the sum

print("sum =",sum)

output:

Enter any number: 10

Enter any number : 100

Enter any number : -999

sum = 110

User Brad Dwyer
by
4.9k points