how to make the main function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hialbert123
    New Member
    • Sep 2014
    • 1

    how to make the main function

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

    Code:
       import tools2         #My code is going the right     thing but just need several statements
        import os      #the requirements are Functions must have 24 statements or less 
        
         [CODE]
    def main(): #and The maximum number of return statements allowed in any function is 2.
    filename = input("Enter filename: ")
    while not os.path.isfile( filename):
    print("File doesn't exist!")
    filename = input("Enter filename: ")
    infile = open(filename)
    lanes = infile.readline s()
    years = []
    for lane in lanes:
    weeks = lane.split(',')
    days = weeks[0]
    year = int(days[0:4])
    if year not in years:
    years.append(ye ar)
    print('Years in file =', years)
    year_q = input("Enter year to summarise (q to quit): ")
    while not year_q == 'q' and int(year_q) not in years:
    print("Year not in file!")
    print('Years in file =', years)
    year_q = input("Enter year to summarise (q to quit): ")
    while year_q != 'q':
    year_q = int(year_q)
    years_lanes = []
    for lane in lanes:
    split = lane.split(',')
    day_to = split[0]
    day_month = int(day_to[4:6])
    daily = int(day_to[0:4])
    if daily == year_q:
    years_lanes.app end(lane)
    month_rs = [0]*13
    month_qs = [0]*13
    for month in range(1,13):
    month_revenue = 0
    number = 0
    for lane in years_lanes:
    split = lane.split(',')
    day_to = split[0]
    day_month = int(day_to[4:6])
    daily = int(day_to[0:4])
    if day_month == month:
    lane = split[1]
    count = split[2]
    price = tools2.letter_p rice (lane, count)
    quilty = int(split[3])
    Revenue = price * quilty
    month_revenue = month_revenue + Revenue
    month_rs[month] = month_revenue
    print()
    print()
    print('Summary for', year_q)
    print()
    print('{:>6}{:> 16}'.format('Mo nth','Total Revenue'))
    for month in range(1, 13):
    print('{:>6}{:> 16.2f}'.format (month,month_rs[month]))
    year_revenue = 0
    for month in range(1,13):
    year_revenue = year_revenue + month_rs[month]
    print('{:>6}{:> 16.2f}'.format( 'TOTAL',year_re venue))
    print()
    print()
    print()
    print('Years in file =', years)
    year_q = input("Enter year to summarise (q to quit): ")
    while not year_q == 'q' and int(year_q) not in years:
    print("Year not in file!")
    print('Years in file =', years)
    year_q = input("Enter year to summarise (q to quit): ")
    main()

    So this is what I done so far and the result is correct. However the other requirments are

    •Functions must have 24 statements or less.

    •The maximum number of return statements allowed in any function is 2.

    so that mean I need to do the several statements to complete this code :(
    But I tried to slove the problem but failed :(

    here is my file:
    filename : sales0.txt

    and content is :
    20101113,p,para uri,3120120830, s,kikorangi,982 0130105,z,waipo roporo,39201410 27,r,kowhai,12

    so for example the 20101113,p,para uri,31 means 31 parauri p's were sold on the 13th day of the 11th month in 2010.
    and to cauclute the letter_price this is the fountion is :

    import hashlib
    def letter_price(le tter, colour):
    """ Returns the price of a letter in dollars, as a float """
    if letter == " ":
    price = 0
    else:
    digest = hashlib.sha1((l etter+colour).e ncode("utf8", "ignore")).hexd igest()
    hex_str = "0x" + digest[-4:]
    price = (int(hex_str, 16) % 300 + 100) / 100
    return price
    [/code]
    any suggestion whould be appreciate :)
    Last edited by Rabbit; Sep 25 '14, 04:35 AM. Reason: Fixed code tags
Working...