質問

So I need help with programming.

My assignment is this:

Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use raw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.

I did this:

inp = raw_input ('Enter Hours: ')
hours = float(inp)
inp = raw_input ('Enter Rate: ')
rate = float(inp)

print rate, hours

if hours <= 40 :
   pay = hours * rate
else :
   pay = rate * 40 + (rate * 1.5 * ( hours - 40 ))

print pay

And it seemed to be okay but when I click on check the code, I enter hours 45, and then rate I tried entering 10.50, 10.5 but every time I get this: 10.5 45.0 ← Mismatch 498.75

The answer 498.75 is correct but I keep getting mismatch there so I cannot finish my assignment. Anyone knows what am i doing wrong?

役に立ちましたか?

解決

To print float with your format you should use format string (examples).

So you should change line:

print rate, hours

to:

print("Rate = %.2f, Hours = %.0f" % (rate, hours))
#               ^             ^
#               |             Remove all chars after point (may be you need to change that 
#                                                           according your task) 
#               Use to chars after comma (no zeros removing)

他のヒント

By using a function you can do it

def computepay(h,r):
    if (h>40) : 
        pay = (40*r)+(h-40)*1.5*r
    else:
        pay = (h*r)     
    return pay
try:
    inp = raw_input("Please enter hours: ")
    hours=float(inp)
    inp = raw_input("Please enter rate: ")
    rate= float(inp)
except:
    print "Please enter a number as input"
    quit()

print computepay(hours,rate)

It seems that print rate, hours produces output which the checking program does not expect, and cannot cope with. Simply comment out that line.

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay  = h * r     
print pay

This would be the answer to your question @user3578390

4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay 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:
        return h * r
    elif h > 40:
        return (40 * r + ((h - 40) * 1.5 * r))

hrs = float(input("Enter Hours:"))
rate = float(input("Enter Rate:"))
p = computepay(hrs, rate)
print("Pay",p)

I did this:

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter rate:")
r = float(rate)
pay  = h * r 

if h <=40:
    pay  = h * r  
else:
    pay = r * 40 + (r * 1.5 * ( h - 40 ))
    print pay
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top