[Tutor] Need some help
Alan Gauld
alan.gauld at btinternet.com
Sat May 7 14:05:33 CEST 2011
"Aaron Brown" <brownam at gmail.com> wrote
> Here is the code I have, and the error.
Actually you have a lot of errors!
> I don't understand the TUPLE problem.
As Steven pointed out you cannot assign to a
tuple once it has vbeen created. You can only
create a new tuple. If you want to change the
collection use a list instead.
> def main():
> notGreenCost = (12,float)
> goneGreenCost = (12, float)
> savings = (12, float)
I'm not sure why you have the float there. That is
a reference to the "function" used for converting
values to floating point. But you never use it....
Are you coming from some other language where
you dimension an array and tell it the type or something?
That doesn't work (or even mean anything) in Python
since collections can hold any type and can grow
dynamically.
> months = "January ","February","March ","April ","May ","June
> ","July
> ","August ","September ","October ","November ","December "
> for index in range(12):
> print "Enter NOT GREEN energy costs for ", months[index]
> notGreenCost[index]=raw_input("Enter now-> ")
Even if you used a list this would store a string in the
list and would only work for the first two elements since
you cannot assign to an index that doesn't exist.
You probably want to use the append method of
a list:
notGreenCost = [] # an empty list
....
for index in range(len(months)):
print "Enter NOT GREEN energy costs for ", months[index]
notGreenCost.append( float(raw_input("Enter now-> ")) )
> for index in range(12):
This loops around 12 times for each month in the loop
above. Is that really what you want? Surely this should
just be part of the same outer loop?
> print "Enter GONE GREEN energy costs for ", months[index]
> goneGreenCost[index]=input("Enter now-> ")
And this has all the same issues as the notGreenCost assignment above.
> print "--------------------------------------- "
> print "SAVINGS NOT GREEN GONE GREEN MONTH"
> print "--------------------------------------- "
Again, I don't think this is where you want this
> for index in range(12):
And this will loop for every item in the inner loop. In other wortds
this will get executed 12*12*12 times - a total of 1728 times!
> savings[index] = notGreenCost(index) -
> goneGreenCost[index]
As it stands this tries to subtract two strings. Thats why
you should convert the raw_uinput values to floats.
> print "$ ", savings[index]
> print "$ ", notGreenCost[index]
> print "$ ", goneGreenCost[index]
> print months[index]
This will print something like
$ 20
$ 50
$ 30
March
Is that really the output format you want? I would
have thought adding at least an explanatory label
would be nice?
HTH
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list