Ask your own question, for FREE!
Computer Science 5 Online
OpenStudy (itz_sid):

[Python] I need some help with my coding. For some reason, it isn't including the last number in my list.

OpenStudy (itz_sid):

Here is a picture of the code. Please ignore the top variables. It is for later use.

OpenStudy (itz_sid):

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()

OpenStudy (itz_sid):

@518nad

OpenStudy (poopsiedoodle):

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.

OpenStudy (itz_sid):

I tried putting it after the last input. But then it didnt count the first one.

OpenStudy (poopsiedoodle):

put it in there after ``` rainfall = int(input("Enter the rainfall (1): ")) ```

OpenStudy (poopsiedoodle):

but keep it in the other place as well

OpenStudy (itz_sid):

Then my input validation doesn't work properly, and it takes in negative numbers, even after displaying that negative numbers arent allowed.

OpenStudy (518nad):

im here hi hihiihihi

OpenStudy (itz_sid):

Hi :)

OpenStudy (518nad):

what i need is the question

OpenStudy (518nad):

dont gimme that face

OpenStudy (itz_sid):

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.

OpenStudy (518nad):

oh cuz u appending before last input bruh

OpenStudy (itz_sid):

If I move it after the last input, it doesnt count the first input

OpenStudy (poopsiedoodle):

aiight then put in an else and then do rainlist.append(rainfall)

OpenStudy (518nad):

ya silly

OpenStudy (518nad):

http://prntscr.com/d1jz72

OpenStudy (itz_sid):

within my input validation?

OpenStudy (518nad):

do that copy it twice

OpenStudy (itz_sid):

OpenStudy (518nad):

noo stopp wyd

OpenStudy (itz_sid):

OpenStudy (itz_sid):

Im doing what you saaaaaaid

OpenStudy (518nad):

take it outta else statement

OpenStudy (518nad):

nono just by itself above for loop on its own

OpenStudy (itz_sid):

O.

OpenStudy (518nad):

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)

OpenStudy (itz_sid):

Yayy. It worked.

OpenStudy (518nad):

like this

OpenStudy (518nad):

yay

OpenStudy (itz_sid):

Are there ways I can find the max and min of the list other than using the "max" and "min" functions?

OpenStudy (518nad):

yeah u can code it urself

OpenStudy (itz_sid):

I mean have it in my code if you look at it. But how would i use that with my list?

OpenStudy (itz_sid):

I have it*

OpenStudy (518nad):

wdym use it with ur list, max and min is dont on a list

OpenStudy (518nad):

done*

OpenStudy (518nad):

k so lets say u working rainlist

OpenStudy (itz_sid):

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))

OpenStudy (itz_sid):

Would that work?

OpenStudy (518nad):

much work

OpenStudy (518nad):

why dont u check max and min after all ur input

OpenStudy (itz_sid):

check?

OpenStudy (518nad):

yeah so what u have right now u will have a list of rainfall right

OpenStudy (518nad):

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

OpenStudy (itz_sid):

What does lens() do?

OpenStudy (itz_sid):

oh wait is that length?

OpenStudy (518nad):

yeah maybe length

OpenStudy (518nad):

no its len

OpenStudy (518nad):

you kind of want to avoid using variables from before, incase u make changes to your array

OpenStudy (518nad):

so its good to just recalculate the array length before u do the max and min

OpenStudy (518nad):

notice the 2nd if, since 0 is the lowest value

OpenStudy (518nad):

this is not real max and min

OpenStudy (518nad):

its just trying to be a little efficient

OpenStudy (518nad):

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

OpenStudy (518nad):

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

OpenStudy (518nad):

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

OpenStudy (518nad):

kinda silly though u dont really do python for efficieny, its more just learning algorithms and stuff

OpenStudy (itz_sid):

Hm I see.... Pretty confusing tho. lol

OpenStudy (518nad):

not that python is bad for programming or anythign

OpenStudy (itz_sid):

OpenStudy (itz_sid):

I think the other way might have been easier/better. I actually understood it that way. lol

OpenStudy (itz_sid):

But it isn't displaying my minimum properly. :/

OpenStudy (itz_sid):

Its actually not even checking the first input as a minum

OpenStudy (itz_sid):

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.

OpenStudy (518nad):

u didnt run the 2nd part right

OpenStudy (518nad):

ur runing it in another for loop

OpenStudy (518nad):

so u have a choice here, either check ur list after its built or check while its built

OpenStudy (518nad):

http://prntscr.com/d1kld2

OpenStudy (itz_sid):

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.

OpenStudy (itz_sid):

Now*

OpenStudy (itz_sid):

@518nad Can you explain it to me? :3

OpenStudy (518nad):

k wat do u wanna know

OpenStudy (518nad):

before we start checking the rest, u initialize max and min to first value and index to 1

OpenStudy (518nad):

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

OpenStudy (bonnieisflash1.0):

hello

OpenStudy (joshmorris):

iowg

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!