Home > Software design >  Lists and loops
Lists and loops

Time:10-11

I have to be able to calculate the revenu per month depending on the number of goats sold per month knowing that every month I sell 20% of the goats I had initially in January. When I try to calculate it through my price_list I can only get 400$ and not the 300$ for the corresponding months.

The variable d is there to know whether or not the month starts with a vowel. If it does d= "d'" and if not d="de".

I have been going at it for a week and can't seem to find a solution for neither of my two problems.

Help would be really appreciated !!!

goats=int(input("Enter the number of goats at the beginning of january :"))
    
    year= ["january","february","march","april","may","june","july","august","september","october","november","december"]
    vowels = "aeiouy"
    price_list = [400,400,400,400,300,300,300,300,300,300,400,400]
    d=""
        
    if goats >0:
        for month in range(len(year)):
            goats_left = int(goats*0.8)
            goats_sold = goats - goats_left
            goats = goats_left
            print("At the of the month of {0} {1}  , we will have {2} goats left, monthly revenu: {3} $".format(d,year[month],goats,price_list[i]))
    else:
        print("The number entered is not valid {0}.format(goats))
        goats=int(input("Enter the number of goats at the beginning of january :"))
    
    
    
    
    print("\n")
    print("ANNUAL REVENUE ........................................................ :".format())
    print(goats_sold_list)

CodePudding user response:

My first suggestion would be to use enumerate as a more pythonic way to iterate over your list and get it's index. I also don't understand your use of d anywhere so perhaps clarify how you intended it to be used.

Finally, l don't see i or goats_sold_list declared anywhere.

This should be a starting point for improvement:

goats = int(input("Enter the number of goats at the beginning of january :"))
    
year = ["january","february","march","april","may","june","july","august","september","october","november","december"]
vowels = "aeiou"
price_list = [400,400,400,400,300,300,300,300,300,300,400,400]
d=""
goats_sold_list = []
    
if goats >0:
    for index, month in enumerate(year):
        #int() always rounds down, i.e 0.8 goes to 0
        goats_left = int(goats*0.8)
        goats_sold = goats - goats_left
        goats = goats_left

        goats_sold_list.append(goats_sold)
        print("At the of the month of {0} {1}  , we will have {2} goats left, monthly revenu: {3} $".format(d,month,goats,price_list[index]))
else:
    print("The number entered is not valid {0}.format(goats)")
    goats=int(input("Enter the number of goats at the beginning of january :"))

print("\n")
print("ANNUAL REVENUE ........................................................ :".format())
print(goats_sold_list)

CodePudding user response:

I don't see a definition for [i] in price_list[i], barring some outside definition, I think that should be price_list[month].

I don't see d being set at any point, you might need to have some line of code setting d to some value based on the first letter of the month.

  • Related