d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Help > Just Need A Little Help...
Add Reply New Topic New Poll
Member
Posts: 16,455
Joined: May 2 2007
Gold: 20.17
Jul 7 2015 08:03am
so i had some homework to write a quick program that was relatively easy but for some reason i have an error in the input everything works except listing the months with the array command when inputting the precipitation for that month, i thought i had everything right, and if i take ,months[index] out everything works fine, but i need it to list the month per the months array via the index, is there some reason this cannot work or did i just make a simple error or what? the error is in line 23 if u cant see it right away...

Code
months=[""]*12
months[0] = "January"
months[1] = "February"
months[2] = "March"
months[3] = "April"
months[4] = "May"
months[5] = "June"
months[6] = "July"
months[7] = "August"
months[8] = "September"
months[9] = "October"
months[10] = "November"
months[11] = "December"
rain_fall_array=[0.0]*12
index = 0
total_precip = 0.0
avg_precip = 0.0
lowest_precip = 0.0
highest_precip = 0.0
high_month_name = ""
low_month_name = ""
for index in range (0,12):
rain_fall_array[index]=float(input("Please enter the total Precipitation in Inches for ",months[index]))
total_precip = total_precip+rain_fall_array[index]
print ("Precipitation for",months[index],"is : ",rain_fall_array[index])
index = index+1
print ("==================================================\n")
avg_precip = total_precip/12
print ("The Average Precipitation for the year was: ",avg_precip,"inches")
print ("The Total Precipitation for the year was: ",total_precip,"inches")
index = 0
lowest_precip = avg_precip
while index < 12:
if rain_fall_array[index]>avg_precip:
if rain_fall_array[index]>highest_precip:
highest_precip = rain_fall_array[index]
high_month_name = months[index]
elif rain_fall_array[index]<lowest_precip:
lowest_precip = rain_fall_array[index]
low_month_name = months[index]
index = index+1
print ("==================================================")
print ("The highest Precipitation was in ",high_month_name," which was ",highest_precip," inches")
print ("The lowest Precipitation was in ",low_month_name," which was ",lowest_precip," inches")


This post was edited by Drakwen on Jul 7 2015 08:14am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 7 2015 11:08am
why don't you start by giving us the error you're getting?

can you explain what you're trying to do? i think you want to concatenate the strings...so just concatenate them. i'm guessing you made a typo and used , instead of +

rain_fall_array[index]=float(input("Please enter the total Precipitation in Inches for " + months[index]))

what you're actually doing is passing a second argument to input(message) function. i dont think the function has a second parameter. so it's failing

https://docs.python.org/2/library/functions.html#input

Quote
input([prompt])
Equivalent to eval(raw_input(prompt)).


/edit: oh, i see. you're confused because with the print function you can "concatenate" strings with commas. that's not technically true. the print function accepts many parameters, and it will concatenate them together. so what's really happening is that you're passing multiple parameters; you're not concatenating. you just assumed that the input function behaves the same way. it does not. so you need to concatenate strings yourself via +. personally, i recommend not passing multiple strings to the print function like that. concatenate them yourself via +

This post was edited by carteblanche on Jul 7 2015 11:19am
Member
Posts: 16,455
Joined: May 2 2007
Gold: 20.17
Jul 7 2015 04:56pm
Quote (carteblanche @ Jul 7 2015 12:08pm)
why don't you start by giving us the error you're getting?

can you explain what you're trying to do? i think you want to concatenate the strings...so just concatenate them. i'm guessing you made a typo and used , instead of +

rain_fall_array[index]=float(input("Please enter the total Precipitation in Inches for " + months[index]))

what you're actually doing is passing a second argument to input(message) function. i dont think the function has a second parameter. so it's failing

https://docs.python.org/2/library/functions.html#input



/edit: oh, i see. you're confused because with the print function you can "concatenate" strings with commas. that's not technically true. the print function accepts many parameters, and it will concatenate them together. so what's really happening is that you're passing multiple parameters; you're not concatenating. you just assumed that the input function behaves the same way. it does not. so you need to concatenate strings yourself via +. personally, i recommend not passing multiple strings to the print function like that. concatenate them yourself via +


idk where you get your info but this is how i fixed it

Code
rain_fall_array[index]=float(input("Please enter the total Precipitation in Inches for {} ".format(months[index])))
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll