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.
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
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.
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of each word A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data as described below. Variables to hold the data read in using std::cin and a return statement. #include <iostream > int main(int argc, char *argv[]) { .... your code goes here }//main Example usage: >A01.exe Enter three 3-letter space separated words, then...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT