(Python) I need help finding the Minimum using the for loop.
Here is my code... def main(): #Prompt User to Enter Total Rainfall for each Month. greatest = 0 smallest = 0 maxRain = 0 minRain = 0 counter = 1 while True: try: months = int(input("How many months? ")) counter = 1 while months < 0 : print("Invalid Input (No Negative Numbers)") months = int(input("How many months? ")) except ValueError: print("Invalid Input") continue else: break sum = 0.0 for i in range (1, months + 1): rainfall = int(input("Enter the rainfall (" + str(i) + "): ")) sum += rainfall if(months == 1): greatest = rainfall smallest = rainfall if(rainfall > greatest): greatest = rainfall maxRain = i if (rainfall < smallest): smallest = rainfall minRain = i print(" Minimum = ", smallest) average = sum/months print("The total rainfall is %.2f" %sum) print("The average monthly rainfall is %.2f" %average) print("The Greatest Rainfall was in month %d and had a rainfall amount of %.2f" %(maxRain, greatest)) print("The Smallest Rainfall was in month %d and had a rainfall amount of %.2f" %(minRain, smallest)) main()
I have tried another way as well, but have failed...
Here is my code: def main(): #Prompt User to Enter Total Rainfall for each Month. greatest = 0 smallest = 0 maxRain = 0 minRain = 0 counter = 1 while True: try: months = int(input("How many months? ")) counter = 1 while months < 0 : print("Invalid Input (No Negative Numbers)") months = int(input("How many months? ")) except ValueError: print("Invalid Input") continue else: break sum = 0.0 for i in range (1, months + 1): rainfall = int(input("Enter the rainfall (" + str(i) + "): ")) sum += rainfall if(months == 1): greatest = rainfall smallest = rainfall if(rainfall > greatest): greatest = rainfall maxRain = i if (rainfall < greatest): smallest = rainfall minRain = i average = sum/months print("The total rainfall is %.2f" %sum) print("The average monthly rainfall is %.2f" %average) print("The Greatest Rainfall was in month %d and had a rainfall amount of %.2f" %(maxRain, greatest)) print("The Smallest Rainfall was in month %d and had a rainfall amount of %.2f" %(minRain, smallest)) main()
i havent learned Python yet sorry i do know bootstrap, html and some javascript
Try taking out: ``` if(months == 1): greatest = rainfall smallest = rainfall ``` That code is just a hinderance, and may be your problem. Try putting in some print statements in for debugging. I've been having good success with writing my comments before writing any code to frame out what the various blocks (actually, more for functions and classes) of code are supposed to do and how they interface.
whats the actual question
oh yep like the guy said before me.. ur problem is if(months == 1): that should read if (i==1):
Thanks!
Join our real-time social learning platform and learn together with your friends!