What's new

Python Pa help po sa activity about loops

BrigthWin

Forum Veteran
Elite
Joined
Jul 21, 2020
Posts
1,638
Reaction
1,768
Points
650
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.
 

Similar threads

Back
Top