Finding maximum value in a loop
16,658
I will try to keep my answer as clean and simple as possible:
value = int(input("Enter the number of values to process: "))
first_value = float(input("First value: "))
total = first_value
maximum = first_value
for i in range(1, value):
next_value = float(input("Next value: "))
total += next_value
if maximum <= next_value:
maximum = next_value
print("The total is {:.1f}".format(total))
print("The maximum is {:.1f}".format(maximum))
Author by
Alex Vincent
Updated on June 04, 2022Comments
-
Alex Vincent almost 2 years
This is just an introductory class code, and i'm wondering how to find the max of all the next_value variable and compare it to the first_value to print the maximum. My if statement is close but I'm not sure how to fix it
maximum = 0.0 value = int(input("Enter the number of values to process: ")) first_value = float(input("First value: ")) next_value_total = 0 for i in range(1, value): next_value = float(input("Next value: ")) next_value_total += next_value if first_value <= next_value: maximum = next_value elif first_value > next_value: maximum = first_value total = next_value_total + first_value print("The total is {:.1f}".format(total)) print("The maximum is {:.1f}".format(maximum))