[Python] I need some help with my coding. For some reason, it isn't including the last number in my list.
Here is a picture of the code. Please ignore the top variables. It is for later use.
Here is the Code if you guys need it. def main(): #Constant variables greatest = 0 smallest = 0 maxRain = 0 minRain = 0 counter = 1 sum = 0.0 rainList = [] #Input validation #Promp user to enter the number of months while True: try: months = int(input("How many months? ")) while months < 0 : print("Invalid Input (No Negative Numbers)") months = int(input("How many months? ")) except ValueError: print("Invalid Input") continue else: break while True: try: rainfall = int(input("Enter the rainfall (1): ")) if (rainfall < 0): print("Invalid Input (No Negative Numbers)") rainfall = int(input("Enter the rainfall (1): ")) except ValueError: print("Invalid Input") continue else: break for i in range (2, months + 1): rainList.append(rainfall) rainfall = input("Enter the rainfall (" + str(i) + "): ") print("The List is", rainList) #Prompt user to enter the amount of rainfall #Calculate the total #Calculate the average #Calculate the maximum and the minimum #Display the total #Display the average #Display the maximum and the minimum with the months '''for i in range (2, months + 1): rainfall = int(input("Enter the rainfall (" + str(i) + "): ")) sum += rainfall if(months == 1): greatest = rainfall if(smallest == 0): smallest = rainfall if(rainfall > greatest): greatest = rainfall maxRain = i if (rainfall < smallest): 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()
@518nad
It looks like it's because you're not using rainList.append(rainfall) after the last input, so the last number isn't getting added to the list.
I tried putting it after the last input. But then it didnt count the first one.
put it in there after ``` rainfall = int(input("Enter the rainfall (1): ")) ```
but keep it in the other place as well
Then my input validation doesn't work properly, and it takes in negative numbers, even after displaying that negative numbers arent allowed.
im here hi hihiihihi
Hi :)
what i need is the question
dont gimme that face
Lol. I basically have to take the users input. Put it into a list. Then display the sum, average, max, and min... using that list. But right now, I am just focusing on the list. For some reason it isn't accepting my last input.
oh cuz u appending before last input bruh
If I move it after the last input, it doesnt count the first input
aiight then put in an else and then do rainlist.append(rainfall)
ya silly
within my input validation?
do that copy it twice
noo stopp wyd
Im doing what you saaaaaaid
take it outta else statement
nono just by itself above for loop on its own
O.
rainList.append(rainfall) for i in range (2, months + 1): rainfall = input("Enter the rainfall (" + str(i) + "): ") rainList.append(rainfall) print("The List is", rainList)
Yayy. It worked.
like this
yay
Are there ways I can find the max and min of the list other than using the "max" and "min" functions?
yeah u can code it urself
I mean have it in my code if you look at it. But how would i use that with my list?
I have it*
wdym use it with ur list, max and min is dont on a list
done*
k so lets say u working rainlist
rainList.append(rainfall) for i in range (2, months + 1): rainfall = input("Enter the Rainfall (" + str(i) + "): ") rainList.append(rainfall) sum = sum + rainfall if(months == 1): greatest = rainfall if(smallest == 0): smallest = rainfall if(rainfall > greatest): greatest = rainfall maxRain = i if (rainfall < smallest): smallest = rainfall minRain = i average = sum/months print("The List of Rainfall is", rainList) 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))
Would that work?
much work
why dont u check max and min after all ur input
check?
yeah so what u have right now u will have a list of rainfall right
rainlist=[......] max=rainlist[0] min=rainlist[0] for i in range(1,len(rainlist)): if rainlist[i] > max max=rainlist[1] if min !=0 and rainlist[i] < min min=rainlist[i] print max,min
What does lens() do?
oh wait is that length?
yeah maybe length
no its len
you kind of want to avoid using variables from before, incase u make changes to your array
so its good to just recalculate the array length before u do the max and min
notice the 2nd if, since 0 is the lowest value
this is not real max and min
its just trying to be a little efficient
if u start to learn python a bit more u can make max and min a bit more efficient if u knew the min value
like u can run max and min on different proccesses at the same time, and you can make min terminate as soon as it sees a 0, so then ur avoiding a whole process
im not quite sure how the if statement terminates locally in python, if they make it auto break once the condition on min !=0 is met
kinda silly though u dont really do python for efficieny, its more just learning algorithms and stuff
Hm I see.... Pretty confusing tho. lol
not that python is bad for programming or anythign
I think the other way might have been easier/better. I actually understood it that way. lol
But it isn't displaying my minimum properly. :/
Its actually not even checking the first input as a minum
And it is messing up with the number at where the minimum value is located. Like for example in the first picture, it says that 0 for the location, while in the second picture, it gave the correct location as 4.
u didnt run the 2nd part right
ur runing it in another for loop
so u have a choice here, either check ur list after its built or check while its built
Yaaaaay. It worked. Not i need to take some time and understand how it worked. If you could, please explain to me why the variables were set greatest = rainfall smallest = rainfall minRain = 1 maxRain = 1 as so... and what this even means... if(rainfall > greatest): greatest = rainfall maxRain = i if (rainfall < smallest): smallest = rainfall minRain = i My professor showed this to us. But I have no idea what is happening here. I just know that it displays the min and max.
Now*
@518nad Can you explain it to me? :3
k wat do u wanna know
before we start checking the rest, u initialize max and min to first value and index to 1
then ur just checking each value in list, if its more than ur current thats the new max, if its less than ur current min thats the new min, u do that till the end, and ur update index incase theres a new min or max
hello
iowg
Join our real-time social learning platform and learn together with your friends!