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>
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);
Related Questions
Below are seven segments of code, each with a part coloured. Indicate the data type of each coloured part by choosing the correct type of data from the following type.
(a) int
(b) float
(c) bool
(d) str
(e) function
(f) list of int
(g) list of str(i)
if temp < 32 : print ("Freezing")
(ii)
L = ['Hiya', 'Zoya', 'Preet'] print(L[1])
(iii)
M = [] for i in range (3) : M.append(i) print(M)
(iv)
L = ['Hiya', 'Zoya', 'Preet'] n = len(L) if 'Donald' in L[1 : n] : print(L)
(v)
if n % 2 == 0 : print("Freezing")
(vi)
L = inputline.split() while L != ( ) : print(L) L = L[1 :]
(vii)
L = ['Hiya', 'Zoya', 'Preet'] print(L[0] + L[1])
Write the output of the following Python code:
for i in range(2,7,2): print(i*'$')
Predict the output of the following code fragments:
count = 0 while count < 10: print ("Hello") count += 1
Predict the output of the following code fragments:
x = 10 y = 0 while x > y: print (x, y) x = x - 1 y = y + 1