有没有更好的方法来编写python中的这个

时间:2017-04-20 01:51:00

标签: python sorting rows

#The function displayInfo will display the savings,
#the costs prior to going green, the costs after
#going green for the appropriate months

def displayInfo (notGreenCosts, goneGreenCosts, savings, months):
    print
    print '\t' '\t' '\t' 'SAVINGS'
    print '___________________________________________________________'
    print 'SAVINGS' '\t''\t' 'NOT GREEN' '\t' 'GONE GREEN' '\t' 'MONTH'
    print '-----------------------------------------------------------'
    print '$',savings[0], '\t','\t', '$',notGreenCosts[0], '\t','\t', '$',goneGreenCosts[0], '\t','\t', months[0]
    print '$',savings[1], '\t','\t', '$',notGreenCosts[1], '\t','\t', '$',goneGreenCosts[1], '\t','\t', months[1]
    print '$',savings[2], '\t','\t', '$',notGreenCosts[2], '\t','\t', '$',goneGreenCosts[2], '\t','\t', months[2]
    print '$',savings[3], '\t','\t', '$',notGreenCosts[3], '\t','\t', '$',goneGreenCosts[3], '\t','\t', months[3]
    print '$',savings[4], '\t','\t', '$',notGreenCosts[4], '\t','\t', '$',goneGreenCosts[4], '\t','\t', months[4]
    print '$',savings[5], '\t','\t', '$',notGreenCosts[5], '\t','\t', '$',goneGreenCosts[5], '\t','\t', months[5]
    print '$',savings[6], '\t','\t', '$',notGreenCosts[6], '\t','\t', '$',goneGreenCosts[6], '\t','\t', months[6]
    print '$',savings[7], '\t','\t', '$',notGreenCosts[7], '\t','\t', '$',goneGreenCosts[7], '\t','\t', months[7]
    print '$',savings[8], '\t','\t', '$',notGreenCosts[8], '\t','\t', '$',goneGreenCosts[8], '\t','\t', months[8]
    print '$',savings[9], '\t','\t', '$',notGreenCosts[9], '\t','\t', '$',goneGreenCosts[9], '\t','\t', months[9]
    print '$',savings[10], '\t','\t', '$',notGreenCosts[10], '\t','\t', '$',goneGreenCosts[10], '\t','\t', months[10]
    print '$',savings[11], '\t','\t', '$',notGreenCosts[11], '\t','\t', '$',goneGreenCosts[11], '\t','\t', months[11]

2 个答案:

答案 0 :(得分:4)

第一个改进应该是for-loop。

for i in range(12):
    print '$',savings[i], '\t','\t', '$',notGreenCosts[i], '\t','\t', '$',goneGreenCosts[i], '\t','\t', months[i]

答案 1 :(得分:0)

#The function displayInfo will display the savings,
#the costs prior to going green, the costs after
#going green for the appropriate months

def displayInfo (notGreenCosts, goneGreenCosts, savings, months):
    print
    print '\t' '\t' '\t' 'SAVINGS'
    print '___________________________________________________________'
    print 'SAVINGS' '\t''\t' 'NOT GREEN' '\t' 'GONE GREEN' '\t' 'MONTH'
    print '-----------------------------------------------------------'
    for i in range(12):  
      print '$',savings[i], '\t','\t', '$',notGreenCosts[i], '\t','\t', '$',goneGreenCosts[i], '\t','\t', months[i]