Question

In: Computer Science

This phyton program requires you to prints out the total of a series of positive floating-pointnumbers...

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 >

Solutions

Expert Solution

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!


Related Solutions

Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Create a python program that prints the series from 5 to 500.
Create a python program that prints the series from 5 to 500.
Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a program calls each of the three methods described below and prints out the result...
Write a program calls each of the three methods described below and prints out the result of the two methods which return value reverse- a void method that reverses the elements of the array. Print the array before you call the method and after call the method. getMin- an int method that returns the smallest value in the array Sample output array: 22,34,21,35,12,4,2,3,99,81 array in reverse: 81,99,3,2,4,12,35,21,34,22 the smallest number is 2
Write a program that generates and prints out in a neat format all possible truth tables...
Write a program that generates and prints out in a neat format all possible truth tables with three propositional variables p, q, and r. Your program should also number generated truth tables and output the numbers so it is clear how many total tables were generated. Your program output may look like the following (Note: only the beginning of the output is shown): Truth table 1: p q r proposition ----------------- F F F F F F T F F...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT