🔒 Closed Pa help po sa activity about loops

Status
Not open for further replies.

BrigthWin

Forum Veteran
Problem:
Picture yourself as a barista experimenting with various coffee brewing methods. The program should prompt the user to input the number of methods they've tested. Following that, it should iteratively ask for the brewing time for each method. After all the times are inputted, the program ought to reveal the quickest, longest, and mean brewing durations.

Use either for or while loop.

Ito po nasimulan ko diko na alam next.

numOfMethods = int(input("Number of tested methods: "))

for n in range(numOfMethods):
n += 1
brewingTime = float(input(f"Time {n}: "))
 
Here's how you can complete the rest of the program using a for loop:

Python:
numOfMethods = int(input("Number of tested methods: "))

brewingTimes = []  # to store the brewing times

for n in range(numOfMethods):
    n += 1
    brewingTime = float(input(f"Time {n}: "))
    brewingTimes.append(brewingTime)  # add the brewing time to the list

# calculate the quickest, longest, and mean brewing durations
quickest = min(brewingTimes)
longest = max(brewingTimes)
mean = sum(brewingTimes) / numOfMethods

# display the results
print(f"Quickest brewing duration: {quickest}")
print(f"Longest brewing duration: {longest}")
print(f"Mean brewing duration: {mean}")

This code first creates an empty list called brewingTimes to store the brewing times for each method. Inside the loop, it prompts the user to input the brewing time for each method and adds it to the list using the append() function.

After the loop ends, it calculates the quickest, longest, and mean brewing durations by using the min(), max(), and sum() functions respectively.

Finally, it displays the results using the print() function.
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 934
    Views
  • 2
    Participants
Last reply from:
jccarrera

Trending Topics

Online now

Members online
992
Guests online
12,234
Total visitors
13,226

Forum statistics

Threads
2,361,222
Posts
29,409,335
Members
1,058,250
Latest member
sopiv
Back
Top