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.
/* 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++
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT