🐍 Python Pa help po sa activity about loops

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.
 

About this Thread

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

Trending Topics

Online now

Members online
374
Guests online
675
Total visitors
1,049

Forum statistics

Threads
2,283,870
Posts
29,018,621
Members
1,221,660
Latest member
sevenrjdidjs
Back
Top