Python for Everyone

4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.

def computepay(h, r):
    if h<=40:
        p=h*r
    else:
        p=40*r+(h-40)*r*1.5
    return p
hour=input("Enter Hours:")
rate=input("Enter Rate:")
h=float(hour)
r=float(rate)
p=computepay(h, r)
print(p)

Loops and Iteration
Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers
Break Statement
The break statement ends the current loop and jumps to the statement immediately following the loop
Definite Loops
Definite loops (for loops) have explicit iteration variables that change each time through a loop. These iteration variables move through the sequence or set.
for i in [5,4,3,2,1] :
print(i)

To count how many times we execute a loop, we introduce a counter variable that starts at 0 and we add one to it each time through the loop.

a=0
    print('Before', a)
    for thing in [9, 41, 23, 32]:
          a=a+1
          print(a, thing)
     print('After', a)

To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum wach time through the loop

a=0
    print('Before', a)
    for thing in [9, 41, 23, 32]:
          a=a+thing
          print(a, thing)
     print('After', a)

Search Using a Boolean Variable
If we want to search and know if a value was found, we use a variable that starts at False and is set to True as soon as we find what we are looking for

found=False
    print('Before', found)
    for value in [9, 41, 23, 32]:
         if value == 23:
             found = True
         print(found, value)
    print('After', found)

Using None to find the smallest number

smallest=None
    print('Before')
    for value in [9, 41, 23, 32]:
          if smallest is None:
             smallest=value
          elif value < smallest:
             smallest=value
   
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值