如何制作主函数
我正在努力完成主要任务。这是我的代码:
选择 | 换行 | 行号
- 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
- 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.readlines()
- years = []
- for lane in lanes:
- weeks = lane.split(',')
- days = weeks[0]
- year = int(days[0:4])
- if year not in years:
- years.append(year)
- 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.append(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_price (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('Month','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_revenue))
- 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,parauri,3120120830,s,kikorangi,9820130105,z,waiporoporo,3920141027,r,kowhai,12
- 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.
- and to cauclute the letter_price this is the fountion is :
- import hashlib
- def letter_price(letter, colour):
- """ Returns the price of a letter in dollars, as a float """
- if letter == " ":
- price = 0
- else:
- digest = hashlib.sha1((letter+colour).encode("utf8", "ignore")).hexdigest()
- hex_str = "0x" + digest[-4:]
- price = (int(hex_str, 16) % 300 + 100) / 100
- return price
任何建议都将不胜感激:)