Computer Science

Fill in the missing lines of code in the following code. The code reads in a limit amount and a list of prices and prints the largest price that is less than the limit. You can assume that all prices and the limit are positive numbers. When a price 0 is entered the program terminates and prints the largest price that is less than the limit.

#Read the limit
limit = float(input("Enter the limit"))
max_price = 0
# Read the next price
next_price = float(input("Enter a price or 0 to stop:"))
while next_price > 0 :
    <write your code here> 
    #Read the next price 
    <write your code here>
if max_price > 0:
    <write your code here> 
else :
    <write your code here>

Python Funda

38 Likes

Answer

#Read the limit
limit = float(input("Enter the limit"))
max_price = 0
# Read the next price
next_price = float(input("Enter a price or 0 to stop:"))
while next_price > 0 :
    if next_price < limit and next_price > max_price:
        max_price = next_price
    #Read the next price 
    next_price = float(input("Enter a price or 0 to stop:"))
if max_price > 0:
    print("Largest Price =", max_price) 
else :
    print("Prices exceed limit of", limit);

Answered By

22 Likes


Related Questions