115k views
4 votes
Write a program that lets the user enter a series of numbers and calculates the sum of all the numbers entered. the program should start by asking "how many numbers do you have? " . the user enters a number, and the program executes a loop that iterates that many times. each time the loop iterates, it should prompt the user to enter a number. after all the numbers have been entered, the program should display the sum of the numbers. below are two sample runs of the program. in the first sample run, the user enters five numbers, and in the second sample run, the user enters 3 numbers. your program�s output must exactly match the output shown in the sample runs. notice the wording of the messages and the placement of spaces and punctuation. sample run (with user input shown in bold): how many numbers do you have? 5 enter a number: 1 enter a number: 2 enter a number: 3 enter a number: 4 enter a number: 5 sum: 15 sample run (with user input shown in bold): how many numbers do you have? 3 enter a number: 10 enter a number: 20 enter a number: 30 sum: 60

User Spokeadoke
by
7.8k points

1 Answer

3 votes

A program that lets the user enter a series of numbers and calculates the sum of all the numbers.

def calculate_sum_of_numbers():

# Get the number of numbers from the user

num_of_numbers = int(input("How many numbers do you have? "))

# Initialize the sum

total_sum = 0

# Iterate based on the number provided by the user

for i in range(num_of_numbers):

# Get a number from the user and add it to the sum

user_input = float(input("Enter a number: "))

total_sum += user_input

# Display the sum

print("Sum:", total_sum)

# Run the program

calculate_sum_of_numbers()

User Brian Thompson
by
8.4k points

No related questions found