In: Computer Science
This phyton program requires you to prints out the total of a series of positive floating-pointnumbers entered by the user. The user should be prompted with the message "Enter a floating-point number >= 0:". You do not need to check that the input is a FP number. If the user correctly enters a positive FP number, handle it and prompt the user for the next number with the same message. If the user enters a negative number, your program should print "Number must be >= 0. Try again:" and wait for another number. Negative numbers should not be added to the total. If the user enters a zero, that is an indication that the user is finished entering numbers. You program should print out "The sum of the numbers is:" followed by the total. You do not need to round the total. Below are two runs of the program: Enter a floating-point number >= 0: 2.5 Enter a floating-point number >= 0: 3 Enter a floating-point number >= 0: -13.9 Number must be >= 0. Try again: 27 Enter a floating-point number >= 0: 8 Enter a floating-point number >= 0: 0 The sum of the numbers is: 40.5 Enter a floating-point number >= 0: 0 The sum of the numbers is: 0 def sumPositiveFPNumbers(): < your code goes here >
Explanation:
here is the function which uses the while loop to keep asking the numbers from the user until the user does not enter 0.
Now, only positive numbers are accepted and are added whose sum is shown at the end.
Code:
def sumPositiveFPNumbers():
res = 0
while(True):
num = float(input("Enter a floating-point number >= 0: "))
while(num<0):
num = float(input("Number must be >= 0. Try again: "))
if num==0:
break
res = res + num
print("The sum of the numbers is:", res)
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!