Im working on my final project for school where i have to uses arrays to store the first year of energy costs, the second array will store the second year after going green, and the third array will store the difference. I have pretty much all the code written out except for the last part of the print results which just displays the information on screen. How ever I keep getting a syntax error for the variable no_cost just as im about to ask the user for input. Is anyone able to help me wrap this project up?

#Date: December 12th 2011
#Purpose: To create a program that allows users to enter energy bills for a year prior to and after going green.
#Then calculate the savings and display the two years of data + savings to the user.

#global constants
NO_OF_MONTHS= 12
MONTHS_OF_YEAR= ["January","February","March","April","May","June","July","August","September","October","November","December"]

def main ():
    #real not_green_cost, gone_green_cost, savings
    #create 3 arrays to store data
    not_green_cost= [0] * 12
    gone_green_cost=[0] * 12
    savings= [0] * 12
    end_program= "yes"

    while (end_program.lower() == "yes"):
        print ("****This program calculates the energy savings for AK’s College Turned Green****")
        #get the not green energy cost's from the user
        not_green_cost=get_non_green_cost
        #get the green energy cost's from the user
        gone_green_cost=get_green_cost





#############################################################################################
#have user enter bills from january-december for prior to going green
def get_not_green_cost ():
    #real not_cost[NO_OF_MONTHS]
    #int index
    not_cost= [0] * NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

    print ("Enter NOT green costs for %s" %MONTHS_OF_YEAR)
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        not_cost[index]=float (input ("Enter now -->"))
        while (not_cost[index] < MIN_COST or not_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST)
               not_cost [index]=float(input ("Enter now -->"))
    return not_cost
#################################################################################################
#have user enter bills from january-december for after going green
def get_green_cost ():
    #real green_cost[NO_OF_MONTHS]
    #int index
    green_cost= [0] * NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

    print ("Enter GONE green costs for %s" %MONTHS_OF_YEAR)
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        green_cost[index]=float (input ("Enter now -->"))
        while (green_cost[index] < MIN_COST or green_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST)
               green_cost[index]=float(input ("Enter now -->"))
    return green_cost
####################################################################################################
#calculate the savings and return it
def calculate_energy_savings (not_cost, green_cost):
    #int index
    #real savings[NO_OF_MONTHS]
    savings= [0] * NO_OF_MONTHS
    for index in range (NO_OF_MONTHS):
        savings[index]=not_cost[index]-green_cost[index]

    return savings
    
    
#######################################################################################################
#this function prints the results for the 2 years of data as well as displaying the savings
def print_results
        print ("=================SAVINGS==============")

    return
######################################################################################################
main ()

thanks for catching that, still doesnt solve my problem though :(

You have really mixed up your program, for example not_green_cost and green_cost are never called and they are not yet defined in main program anyway as it is before them. Fix the errors interpreter is giving you, for example print_results is completely incorrect syntax. Also your while loop is infinite loop...

from pprint import pprint
costs = [[float(raw_input(' '.join((prompt, month, str(year)))+': ')) for prompt in 'green', 'not green']
	 for month in ["January","February","March","April","May","June","July","August","September","October","November","December"] 
         for year in (1,2)]
costs = [(a, b, b-a) for a,b in costs]
pprint(costs)

Edited 4 Years Ago by pyTony: n/a

Yeah i have just gone back and completely re-worked the code. Its fully functional now as i can have the user input the data. I just need to figure out how to write the print results function so that i could have the data displayed like this:

SAVINGS
_____________________________________________________
SAVINGS NOT GREEN GONE GREEN MONTH
_____________________________________________________

$ 243.00 $ 789.00 $ 546.00 January

$ 254.00 $ 790.00 $ 536.00 February

$ 371.00 $ 890.00 $ 519.00 March

$ 280.00 $ 773.00 $ 493.00 April

#Date: December 12th 2011
#Purpose: To create a program that allows users to enter energy bills for a year prior to and after going green.
#Then calculate the savings and display the two years of data + savings to the user.

#global constants
NO_OF_MONTHS= 12
MONTHS_OF_YEAR= ["January","February","March","April","May","June","July","August","September","October","November","December"]

def main ():
    #real not_green_cost, gone_green_cost, savings
    #create 3 arrays to store data
    not_green_cost=[0]*12
    gone_green_cost=[0]*12
    savings=[0]*12
    end_program= "yes"

    while (end_program.lower() == "yes"):
        print ("****This program calculates the energy savings for AK’s College Turned Green****")
        #get the not green energy cost's from the user
        not_green_cost=get_not_green_cost()
        #get the green energy cost's from the user
        gone_green_cost=get_green_cost()
        #print results to user
        print_results(not_green_cost,gone_green_cost,savings)
    
        




    return 
#############################################################################################
#have user enter bills from january-december for prior to going green
def get_not_green_cost ():
    #real not_cost[NO_OF_MONTHS]
    #int index
    not_cost=[0]*NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

   
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        print ("Enter NOT green costs for %s" %MONTHS_OF_YEAR[index])
        not_cost[index]=float (input ("Enter now -->"))
        while (not_cost[index] < MIN_COST or not_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST))
               not_cost[index]=float(input ("Enter now -->"))
    return not_cost
#################################################################################################
#have user enter bills from january-december for after going green
def get_green_cost ():
    #real green_cost[NO_OF_MONTHS]
    #int index
    green_cost= [0] * NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

    
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        print ("Enter GONE green costs for %s" %MONTHS_OF_YEAR[index])
        green_cost[index]=float (input ("Enter now -->"))
        while (green_cost[index] < MIN_COST or green_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST))
               green_cost[index]=float(input ("Enter now -->"))
    return green_cost
####################################################################################################
#calculate the savings and return it
def calculate_energy_savings (not_cost, green_cost):
    #int index
    #real savings[NO_OF_MONTHS]
    savings= [0] * NO_OF_MONTHS
    for index in range (NO_OF_MONTHS):
        savings[index]=not_cost[index]-green_cost[index]

    return savings
    
    
#######################################################################################################
#this function prints the results for the 2 years of data as well as displaying the savings
#real not_green_cost[NO_OF_MONTHS],gone_green_cost[NO_OF_MONTHS],savings[NO_OF_MONTHS]
def print_results(not_green_cost,gone_green_cost,savings):
        #int index
    
        print ("=================SAVINGS==============")
        print ("-----------------------------------------------")
        print ("SAVINGS     NOT GREEN     GONE GREEN     MONTH")
        print ("-----------------------------------------------")
        for index in range (NO_OF_MONTHS):
            print ("%10s, %10.2f, %14.2f" %(MONTHS_OF_YEAR[index],not_green_cost[index],gone_green_cost[index],savings[index]))

        return
######################################################################################################
main()

Ive have the full working program now, works 100%. Ill post the code incase others would like to view it. Is there a way i could use a get_cost function to do the job of both the get_not_green_cost and get__green_cost function, so that way i don't have repeated code?

#Date: December 12th 2011
#Purpose: To create a program that allows users to enter energy bills for a year prior to and after going green.
#Then calculate the savings and display the two years of data + savings to the user.

#global constants
NO_OF_MONTHS= 12
MONTHS_OF_YEAR= ["January","February","March","April","May","June","July","August","September","October","November","December"]

def main ():
    #real not_green_cost, gone_green_cost, savings
    #create 3 arrays to store data
    not_green_cost=[0]*12
    gone_green_cost=[0]*12
    savings=[0]*12
    end_program= "yes"


    print ("****This program calculates the energy savings for AK’s College Turned Green****")
    #get the not green energy cost's from the user
    not_green_cost=get_not_green_cost()
    #get the green energy cost's from the user
    gone_green_cost=get_green_cost()
    #confirm with user that they have entered all of their data
    end_program=input ("Do you want to end program? (Enter yes or no)")
    while (end_program.lower() == "yes"):
            #print results to user
            print_results(not_green_cost,gone_green_cost,savings)
            end_program= "no"
    
    
    
        




    return 
#############################################################################################
#have user enter bills from january-december for prior to going green
def get_not_green_cost ():
    #real not_cost[NO_OF_MONTHS]
    #int index
    not_cost=[0]*NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

   
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        print ("Enter NOT green costs for %s" %MONTHS_OF_YEAR[index])
        not_cost[index]=float (input ("Enter now -->"))
        while (not_cost[index] < MIN_COST or not_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST))
               not_cost[index]=float(input ("Enter now -->"))
    return not_cost
#################################################################################################
#have user enter bills from january-december for after going green
def get_green_cost ():
    #real green_cost[NO_OF_MONTHS]
    #int index
    green_cost= [0] * NO_OF_MONTHS
    MIN_COST=400
    MAX_COST=1000

    
    for index in range (NO_OF_MONTHS): #0 to NO_OF_MONTHS
        print ("Enter GONE green costs for %s" %MONTHS_OF_YEAR[index])
        green_cost[index]=float (input ("Enter now -->"))
        while (green_cost[index] < MIN_COST or green_cost[index] > MAX_COST):
               print ("Error in input")
               print ("You must enter a value between %.2f and %.2f" %(MIN_COST,MAX_COST))
               green_cost[index]=float(input ("Enter now -->"))
    return green_cost
####################################################################################################
#calculate the savings and return it
def calculate_energy_savings (not_cost, green_cost):
    #int index
    #real savings[NO_OF_MONTHS]
    savings= [0] * NO_OF_MONTHS
    for index in range (NO_OF_MONTHS):
        savings[index]=not_cost[index]-green_cost[index]

    return savings
    
    
#######################################################################################################
#this function prints the results for the 2 years of data as well as displaying the savings
#real not_green_cost[NO_OF_MONTHS],gone_green_cost[NO_OF_MONTHS],savings[NO_OF_MONTHS]
#String MONTHS_OF_YEAR
def print_results(not_green_cost,gone_green_cost,savings):
        #int index
    
        print ("=================SAVINGS==============")
        print ("-----------------------------------------------")
        print ("%8s %15s %15s %12s" %("Month", "Not Green", "Gone Green", "Savings" ))
        print ("-----------------------------------------------")
        for index in range (NO_OF_MONTHS):
            print ("%12s, $%10.2f, $%10.2f, $%10.2f" %(MONTHS_OF_YEAR[index],not_green_cost[index],gone_green_cost[index],savings[index]))

        return
######################################################################################################
main()

Yes, just pass the difference in prompt to function like I did in my list comprehension. Change the fuction name and variable name also generic.

I would suggest you to study list comprehensions and append method to get out of way of writing C in Python.

Edited 4 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.