Thread: Penny Per Day

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Penny Per Day


    Hey guys,

    Second post here. Coming here again for help with my homework... since my professor basically just throws the book at us (which only has psuedo-code, no actual python code) and then gives us these labs. I really appreciate everyone's help, I always make sure to try and work out the lab as much as I can before posting. Here is the lab I'm working on currently:

    Design & write a python program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day and continues to double each day.

    The only input to the program should be the number of days to calculate. The day number, amount of pay for each day and the accumulated amount of pay should be stored in a two dimensional array and the results printed from that array.

    The output should be displayed in a dollar amounts, not the number of pennies.

    A minimum of three modules should be used for this problem.

    Hint: Each day of work would be represented by a row. Column 1 would be the day count, column 2 the current day's pay and column 3 the accumulated pay.

    Here's my code:

    Code:
    accumulatedPay = []
    payPerDay = []
    days = []
    square = []
    
    def numberDays():
        global daysWorked
        daysWorked = (input("Pleaes enter the days worked"))
        return daysWorked
    
    def salaryOnDay(daysWorked):
        return 2**(daysWorked-1)
    
        
    
    
    def Calculations():
        earnings = 0
        for i in range (1, daysWorked + 1):
            salary = salaryOnDay(i)
            days.append(daysWorked)
            payPerDay.append(salary)
            earnings += salary
            accumulatedPay.append(earnings)
        
        
    def main():
        numberDays()
        Calculations()
        for i in range (0, daysWorked):
            square = [days, payPerDay, accumulatedPay]
            print square
            
    main()
    Output: Pleaes enter the days worked3
    [[3, 3, 3], [1, 2, 4], [1, 3, 7]]
    [[3, 3, 3], [1, 2, 4], [1, 3, 7]]
    [[3, 3, 3], [1, 2, 4], [1, 3, 7]]

    Now I know I'm doing something wrong by not assigning values to the rows and columns, but I don't really understand how it works with this assignment even after reading the book. As you can see I have the days, the pennies per day, and the total accumulated pay assigned to the variables correctly. I just can't grasp how to change the columns to 1, 2, 3, 4, 5 day, etc...you get the idea.

    Thanks again, going to drop a letter grade in my class if I don't get this in by midnight.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Could really use your help...still stuck.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I'd remove the global variables (except that I added some constants to name the indexes). Pass the data via return statements to keep it local.

    I believe that storing the sequential index is foolish, yet it was a requirement.

    I changed the calculational dependency. Today's pay and cumulative total is based on yesterday's knowledge. The formula was simple and you understood it so I'm not saying it's a better method. It's a different method---one which more directly matches the problem description.

    This version tests the input.
    Code:
    DAY, INCOME, CUMULATIVE = range(3)
    
    def numberDays():
        while True:
            try:
                result = input("Please enter the days worked: ")
            except:
                pass
            else:
                if (type(result) is int) and (-1 < result):
                    return result
    
    def Calculations(days):
        result = [[0, 0, 0]]
        if 0 < days:
            result.append([1, 1, 1])
        day = 1
        while day < days:
            yesterday = result[-1]
            day = yesterday[DAY] + 1
            today = [None]*3
            today[DAY] = day
            today[INCOME] = yesterday[INCOME] * 2
            today[CUMULATIVE] = yesterday[CUMULATIVE] + today[INCOME]
            result.append(today)
        return result
    
    def display(salary):
        print('{:>4s} {:>13s} {:>13s}'.format('', 'daily', 'cumulative'))
        print('{:>4s} {:>13s} {:>13s}'.format('Day', 'income, $', 'income, $'))
        for record in salary:
            (D, C) = divmod(record[INCOME], 100)
            (d, c) = divmod(record[CUMULATIVE], 100)
            print('{:4d} {:10d}.{:0>2d} {:10d}.{:0>2d}'.format(record[DAY], D, C, d, c))
    
    def main():
        days = numberDays()
        pay_record = Calculations(days)
        display(pay_record)
    
    main()

    Comments on this post

    • williamsjm5 agrees
    Last edited by b49P23TIvg; April 21st, 2013 at 09:14 PM. Reason: simplification
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    Thank you SO very much b49. It's greatly appreciated. I'm not sure I understand most of it, but I'm going to test it and run through each line so I understand it for future programs (with the help of google, of course)

    By the way, I checked out your linkedIn profile and saw that you work for Corning. That's awesome, for a year I worked as an inspector for crews laying fiber. Truly fascinated by optics, but I'm studying geology right now-- prefer to observe things rather than dealing with the stress of being an engineer.

IMN logo majestic logo threadwatch logo seochat tools logo