Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Weekly Pay Table program urgent help


    A Weekly Pay Table

    Write a Python program that calculates the weekly gross pay for pay rates from $12 to $20 per hour, working from 5 to 20 hours per week, and displays the results.

    IMPORTANT
    You MUST use two for-loops, one inside the other.

    Solutions creating the results by adding lines like:
    printNow ('12 hrs/wk: 60 72 84 96 ....');
    printNow ('13 hrs/wk: 65 78 91 104 ....');
    will be awarded zero marks.

    Hints:
    line two of the header (the 5 6 7 ...) can be created using a fixed string (for the above example): ' 5 6 7 8 9 10'
    or another for-loop - whichever you prefer.
    tabs between the items of output can help in getting even spacing.
    get one for-loop (the one that creates a single row) working correctly, then wrap it in another for-loop, that creates new rows.
    e.g. - you could have a variable called hoursPerWeek and set it to 5.
    - use a single for-loop to create the first row of the table.
    - once this is going, take the for-loop statement and surround it with another for-loop that
    changes hoursPerWeek.

    Im struggling to even start this assignment, please help. Ive been trying my hardest but it seems to difficult to start this
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by kidoman
    Im struggling to even start this assignment, please help. Ive been trying my hardest but it seems to difficult to start this
    Start by writing pseudocode for what you want to achieve. Then replace each part of the pseudocode with proper python code.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by partoj
    Start by writing pseudocode for what you want to achieve. Then replace each part of the pseudocode with proper python code.
    what do you mean?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by kidoman
    what do you mean?
    I mean that, if you have a task that seems daunting and unapproachable, split it up into smaller tasks, and then create pseudocode for each task.

    Say that I wanted to capitalize every word in a list, the pseudocode would looke somewhat like this

    Code:
    list = word1, word2, word3
    
    for every "item" in the list "list" do
        capitalize "item" and assign it to "new_item"
        print "new_item"
    Then I 'convert' it to python:

    Code:
    list = ['word', 'another', 'third']
    
    for item in list:
        new_item = item.upper()
        print new_item
    http://www.catb.org/esr/faqs/smart-questions.html#code
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    Code:
    range =: (<. + i.@:>:@:|@:-)/
    ('';'';,.'wage'),.('';'weekly hours'),(range 12 20) *table (range 5 20)
    ┌─┬──┬───────────────────────────────────────────────────────────────┐
    │ │  │weekly hours                                                   │
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │ │* │  5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20│
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │w│12│ 60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240│
    │a│13│ 65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260│
    │g│14│ 70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280│
    │e│15│ 75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300│
    │ │16│ 80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320│
    │ │17│ 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340│
    │ │18│ 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360│
    │ │19│ 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380│
    │ │20│100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400│
    └─┴──┴───────────────────────────────────────────────────────────────┘
    www.jsoftware.com
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    range =: (<. + i.@:>:@:|@:-)/
    ('';'';,.'wage'),.('';'weekly hours'),(range 12 20) *table (range 5 20)
    ┌─┬──┬───────────────────────────────────────────────────────────────┐
    │ │  │weekly hours                                                   │
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │ │* │  5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20│
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │w│12│ 60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240│
    │a│13│ 65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260│
    │g│14│ 70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280│
    │e│15│ 75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300│
    │ │16│ 80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320│
    │ │17│ 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340│
    │ │18│ 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360│
    │ │19│ 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380│
    │ │20│100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400│
    └─┴──┴───────────────────────────────────────────────────────────────┘
    www.jsoftware.com
    What is this code?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    range =: (<. + i.@:>:@:|@:-)/
    ('';'';,.'wage'),.('';'weekly hours'),(range 12 20) *table (range 5 20)
    ┌─┬──┬───────────────────────────────────────────────────────────────┐
    │ │  │weekly hours                                                   │
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │ │* │  5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20│
    ├─┼──┼───────────────────────────────────────────────────────────────┤
    │w│12│ 60  72  84  96 108 120 132 144 156 168 180 192 204 216 228 240│
    │a│13│ 65  78  91 104 117 130 143 156 169 182 195 208 221 234 247 260│
    │g│14│ 70  84  98 112 126 140 154 168 182 196 210 224 238 252 266 280│
    │e│15│ 75  90 105 120 135 150 165 180 195 210 225 240 255 270 285 300│
    │ │16│ 80  96 112 128 144 160 176 192 208 224 240 256 272 288 304 320│
    │ │17│ 85 102 119 136 153 170 187 204 221 238 255 272 289 306 323 340│
    │ │18│ 90 108 126 144 162 180 198 216 234 252 270 288 306 324 342 360│
    │ │19│ 95 114 133 152 171 190 209 228 247 266 285 304 323 342 361 380│
    │ │20│100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400│
    └─┴──┴───────────────────────────────────────────────────────────────┘
    www.jsoftware.com
    After trying for a long time, i have this code to start with:

    Code:
    def myweeklyPay():
        mylist=["5" + "\t6" + "\t7" + "\t8" + "\t9" + "\t10" + "\t11"]
        for i in mylist:
          printNow(i)
    The numbers are just to check if they line up which they do
    What do i do after this?
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    To solve the problem you essentially need to create a multiplication table. In j (www.jsoftware.com) that's accomplished with *table to which I attached some labels thereby giving you a target output.
    Code:
    >>> for i in range(3,6+1): # i from 3 to 6 inclusively
    ...   for j in range(9,13+1): # j takes all values from 9 to 13 inclusively
    ...     print('\tf({:2d}, {:2d})'.format(i, j), end = '') # python3 print function that doesn't insert a new line
    ...   print() # python3 function that does show a new line
    ... 
    	f( 3,  9)	f( 3, 10)	f( 3, 11)	f( 3, 12)	f( 3, 13)
    	f( 4,  9)	f( 4, 10)	f( 4, 11)	f( 4, 12)	f( 4, 13)
    	f( 5,  9)	f( 5, 10)	f( 5, 11)	f( 5, 12)	f( 5, 13)
    	f( 6,  9)	f( 6, 10)	f( 6, 11)	f( 6, 12)	f( 6, 13)
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    To solve the problem you essentially need to create a multiplication table. In j (www.jsoftware.com) that's accomplished with *table to which I attached some labels thereby giving you a target output.
    Code:
    >>> for i in range(3,6+1): # i from 3 to 6 inclusively
    ...   for j in range(9,13+1): # j takes all values from 9 to 13 inclusively
    ...     print('\tf({:2d}, {:2d})'.format(i, j), end = '') # python3 print function that doesn't insert a new line
    ...   print() # python3 function that does show a new line
    ... 
    	f( 3,  9)	f( 3, 10)	f( 3, 11)	f( 3, 12)	f( 3, 13)
    	f( 4,  9)	f( 4, 10)	f( 4, 11)	f( 4, 12)	f( 4, 13)
    	f( 5,  9)	f( 5, 10)	f( 5, 11)	f( 5, 12)	f( 5, 13)
    	f( 6,  9)	f( 6, 10)	f( 6, 11)	f( 6, 12)	f( 6, 13)
    Thanks for the reply, i still have no idea on how to put that multiplication table you gave me on the program.

    i have this starting code:
    Code:
    def myweeklyPay(hoursPerWeek):
        mylist=["\t\tPay Rate: Dollars per Hour""\n\t5" + "\t6" + "\t7" + "\t8" + "\t9" + "\t10""\n ========================================="]
        for i in mylist:
          printNow(i)
          
          
          for i in range(1):
            printNow("%d %s" % (hoursPerWeek, "hrs/wk:"))
    Now i need to insert the multiplication table and edit it, please help
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    To solve the problem you essentially need to create a multiplication table. In j (www.jsoftware.com) that's accomplished with *table to which I attached some labels thereby giving you a target output.
    Code:
    >>> for i in range(3,6+1): # i from 3 to 6 inclusively
    ...   for j in range(9,13+1): # j takes all values from 9 to 13 inclusively
    ...     print('\tf({:2d}, {:2d})'.format(i, j), end = '') # python3 print function that doesn't insert a new line
    ...   print() # python3 function that does show a new line
    ... 
    	f( 3,  9)	f( 3, 10)	f( 3, 11)	f( 3, 12)	f( 3, 13)
    	f( 4,  9)	f( 4, 10)	f( 4, 11)	f( 4, 12)	f( 4, 13)
    	f( 5,  9)	f( 5, 10)	f( 5, 11)	f( 5, 12)	f( 5, 13)
    	f( 6,  9)	f( 6, 10)	f( 6, 11)	f( 6, 12)	f( 6, 13)
    Code:
     def myweeklyPay(hoursPerWeek):
        mylist=["\t\t\tPay Rate: Dollars per Hour""\n\t\t12" + "\t13" + "\t14" + "\t15" + "\t16" + "\t17" + "\t18" + "\t19" + "\t20""\n ======================================================================"]
        for i in mylist:
          printNow(i)
          
        for i in range(1):
            printNow("%d %s \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d " % (hoursPerWeek, "hrs/wk:", hoursPerWeek*12, hoursPerWeek*13, hoursPerWeek*14, hoursPerWeek*15, hoursPerWeek*16, hoursPerWeek*17, hoursPerWeek*18, hoursPerWeek*19, hoursPerWeek*20))
    everytime i use this function it starts all the way from the top,
    for example:

    Code:
    myweeklyPay(10)
    myweeklyPay(15)
    			 Pay Rate: Dollars per Hour
    		12	13	14	15	16	17	18	19	20
     ======================================================================
    10 hrs/wk: 	120 	130 	140 	150 	160 	170 	180 	190 	200 
    			Pay Rate: Dollars per Hour
    		12	13	14	15	16	17	18	19	20
     ======================================================================
    15 hrs/wk: 	180 	195 	210 	225 	240 	255 	270 	285 	300
    everytime i use myweeklyPay() it creates this whole thing, how can i get it to show:?
    Code:
    Pay Rate: Dollars per Hour
    		12	13	14	15	16	17	18	19	20
     ======================================================================
    10 hrs/wk: 	120 	130 	140 	150 	160 	170 	180 	190 	200 
    15 hrs/wk: 180 	195 	210 	225 	240 	255 	270 	285 	300
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    Many solutions.

    You could have myweeklyPay save its state. It could test to see if it already displayed the heading.
    • def myweeklyPay(hours, state=[]):
    • global state

    You could pass the information via an argument

    def myweeklyPay(
    hours:numbers.Number,
    show_heading:bool):

    The global state is not a good plan. Classes are designed to maintain state through attributes. Classes are a good way to group related but separate tasks. We're now heading toward a solution I prefer.

    Displaying the heading is a different operation from showing the body of the table. They're separate tasks. Put them in different, simple functions.

    def show_heading(arguments, as, necessary):
    #whatever

    Use myweeklyPay almost as is, but remove the heading carp.
    [code]Code tags[/code] are essential for python code and Makefiles!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Many solutions.

    You could have myweeklyPay save its state. It could test to see if it already displayed the heading.
    • def myweeklyPay(hours, state=[]):
    • global state

    You could pass the information via an argument

    def myweeklyPay(
    hours:numbers.Number,
    show_heading:bool):

    The global state is not a good plan. Classes are designed to maintain state through attributes. Classes are a good way to group related but separate tasks. We're now heading toward a solution I prefer.

    Displaying the heading is a different operation from showing the body of the table. They're separate tasks. Put them in different, simple functions.

    def show_heading(arguments, as, necessary):
    #whatever

    Use myweeklyPay almost as is, but remove the heading carp.
    How do i do it in classes? because we havent got taught that yet..can u show me in code form in the argument way?
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    Code:
    class Stateful:
        def __init__(self):
            self.state = 0
        def __call__(self):
            if self.state == 0:
                print('head')
                self.state += 1
            print('a row')
    
    if __name__ == '__main__':
        o = Stateful()
        o()
        o()
        o()
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Many solutions.

    You could have myweeklyPay save its state. It could test to see if it already displayed the heading.
    • def myweeklyPay(hours, state=[]):
    • global state

    .
    can you please show me how to have myweeklyPay save its state in code form? im so lost

    heres my code:
    Code:
    def myweeklyPay(hoursPerWeek):
        mylist=["\t\t\tPay Rate: Dollars per Hour""\n\t\t12" + "\t13" + "\t14" + "\t15" + "\t16" + "\t17" + "\t18" + "\t19" + "\t20""\n ======================================================================"]
        for i in mylist:
          printNow(i)
          
        for i in range(1):
            printNow("%d %s \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d " % (hoursPerWeek, "hrs/wk:", hoursPerWeek*12, hoursPerWeek*13, hoursPerWeek*14, hoursPerWeek*15, hoursPerWeek*16, hoursPerWeek*17, hoursPerWeek*18, hoursPerWeek*19, hoursPerWeek*20))
    
    myweeklyPay(10)
    myweeklyPay(15)
  28. #15
  29. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    Code:
    printNow = print
    def myweeklyPay(hoursPerWeek, state = []):
        if not state:
            print("\t\t\tPay Rate: Dollars per Hour\n\t\t12\t13\t14\t15\t16\t17\t18\t19\t20\n ======================================================================")
            state.append('done')
        for i in range(1):
            print("%d %s \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d \t%d " % (hoursPerWeek, "hrs/wk:", hoursPerWeek*12, hoursPerWeek*13, hoursPerWeek*14, hoursPerWeek*15, hoursPerWeek*16, hoursPerWeek*17, hoursPerWeek*18, hoursPerWeek*19, hoursPerWeek*20))
    [code]Code tags[/code] are essential for python code and Makefiles!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo