By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

how to make the main function

P: 1
I am struggle to make the main founction.
here is my code:

Expand|Select|Wrap|Line Numbers
  1.    import tools2         #My code is going the right     thing but just need several statements
  2.     import os      #the requirements are Functions must have 24 statements or less 
  3.  
  4.         def main():    #and The maximum number of return statements allowed in any function is 2.
  5.             filename = input("Enter filename: ")
  6.             while not os.path.isfile(filename):
  7.             print("File doesn't exist!")
  8.                 filename = input("Enter filename: ")
  9.             infile = open(filename)
  10.             lanes = infile.readlines()
  11.             years = []
  12.             for lane in lanes:
  13.                  weeks = lane.split(',')
  14.                  days = weeks[0]
  15.                  year = int(days[0:4])
  16.                  if year not in years:
  17.                      years.append(year)
  18.             print('Years in file =', years)
  19.             year_q = input("Enter year to summarise (q to     quit): ")
  20.             while not year_q == 'q' and int(year_q) not in     years:
  21.                 print("Year not in file!")
  22.                 print('Years in file =', years)
  23.                 year_q = input("Enter year to summarise (q to quit): ")
  24.             while year_q != 'q':
  25.                 year_q = int(year_q)
  26.                 years_lanes = []
  27.                 for lane in lanes:
  28.                     split = lane.split(',')
  29.                     day_to = split[0]
  30.                     day_month = int(day_to[4:6])
  31.                     daily = int(day_to[0:4])
  32.                     if daily == year_q:
  33.                         years_lanes.append(lane)
  34.                 month_rs = [0]*13
  35.                 month_qs = [0]*13
  36.                 for month in range(1,13):
  37.                     month_revenue = 0
  38.                     number = 0
  39.                     for lane in years_lanes:
  40.                         split = lane.split(',')
  41.                         day_to = split[0]
  42.                         day_month = int(day_to[4:6])
  43.                         daily = int(day_to[0:4])
  44.                         if day_month == month:
  45.                             lane = split[1]
  46.                             count = split[2]
  47.                             price = tools2.letter_price    (lane, count)
  48.                             quilty = int(split[3])
  49.                             Revenue = price * quilty
  50.                             month_revenue = month_revenue + Revenue
  51.                     month_rs[month] = month_revenue
  52.                 print()
  53.                 print()
  54.                 print('Summary for', year_q)
  55.                 print()
  56.                 print('{:>6}{:>16}'.format('Month','Total     Revenue'))
  57.                 for month in range(1, 13):
  58.                     print('{:>6}{:>16.2f}'.format    (month,month_rs[month]))
  59.                 year_revenue = 0 
  60.                 for month in range(1,13):
  61.                     year_revenue = year_revenue + month_rs[month]
  62.                 print('{:>6}{:>16.2f}'.format('TOTAL',year_revenue))
  63.                 print()
  64.                 print()
  65.                 print()
  66.                 print('Years in file =', years)
  67.                 year_q = input("Enter year to summarise (q to quit): ")
  68.                 while not year_q == 'q' and int(year_q) not in years:
  69.                     print("Year not in file!")
  70.                     print('Years in file =', years)
  71.                     year_q = input("Enter year to     summarise (q to quit): ")
  72.            main()
  73.  
  74. So this is what I done so far and the result is correct. However the other requirments are 
  75.  
  76. •Functions must have 24 statements or less. 
  77.  
  78. •The maximum number of return statements allowed in any function is 2.
  79.  
  80. so that mean I need to do the several statements to complete this code :(
  81. But I tried to slove the problem but failed :(
  82.  
  83. here is my file:
  84. filename : sales0.txt
  85.  
  86. and content is :
  87. 20101113,p,parauri,3120120830,s,kikorangi,9820130105,z,waiporoporo,3920141027,r,kowhai,12
  88.  
  89. so for example the  20101113,p,parauri,31 means 31 parauri p's were sold on the 13th day of the 11th month in 2010.
  90. and to cauclute the letter_price this is the fountion is :
  91.  
  92.     import hashlib
  93.     def letter_price(letter, colour):
  94.         """ Returns the price of a letter in dollars, as a float """
  95.         if letter == " ":
  96.             price = 0
  97.         else:
  98.             digest = hashlib.sha1((letter+colour).encode("utf8", "ignore")).hexdigest()
  99.             hex_str = "0x" + digest[-4:]
  100.             price = (int(hex_str, 16) % 300 + 100) / 100
  101.         return price
  102.  
any suggestion whould be appreciate :)
Sep 25 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.