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