Write a Python 3 program that prompts the user for 3 postive numbers (or zero) and then adds them together. If the user enters a negative number, the program should reprompt them until they enter a postive number. Divide your program into two functions: prompt_number - Prompts for a number, and keeps re-prompting as long as it is negative. Then, returns the number. compute_sum - Accepts 3 numbers, adds them together and returns the sum. main - Calls the prompt_number function 3 times, saves the return value into three different variables, then passes those 3 variables to the compute_sum function. Finally, saves the result of compute_sum into a variable and displays it.

Respuesta :

Answer:

def prompt_number():

   while True:

       number = int(input("Enter a number: "))

       if number >= 0:

           break

       

   return number

   

def compute_sum(n1, n2, n3):

   total = n1 + n2 + n3

   return total

   

n1 = prompt_number()

n2 = prompt_number()

n3 = prompt_number()

result = compute_sum(n1, n2, n3)

print(result)

Explanation:

Create a function named prompt_number that asks the user to enter a number until a positive number or 0 is entered and returns the number

Create a function named compute_sum that takes three numbers, sums them and returns the sum

Ask the user to enter three numbers, call the prompt_number() three times and assign the values

Calculate the the sum, call the compute_sum and pass the numbers as parameters

Print the result

RELAXING NOICE
Relax