Basic Python question about input and conversion

I´m working through an online class in Python and I´m stuck on a problem. The problem asks for an algorithm that produces data for a worker which tabulates the hours worked (regular hours 40/week) and rate of pay(10.50/hr). If the hours are over 40, then the rate of pay is increased by 1.5.

I output the correct amount; however, Python tells me that You must read the rate using input() and then convert it.

Read the rate using input I interpret as allowing input to provide me with the data BEFORE I convert it to a float. Am I correct? Incorrect? If so, what is the issue?

hrs = input("Enter Hours:")
rate = input("Enter Rate:")

#the hourly rate for the hours up to 40
reg = 40.0*10.5
#1.5 times the hourly rate for all hours worked above 40 hours
otr = float(rate)*1.5
oth = float(hrs) - 40
ot = ((oth * otr) + reg)

if float(hrs) > 40:
1 Like

Yes, I think that’s what it try to point out. You will need to get the data for rate from an input.

Buh , It seems you are writing your solution on your online class editor, I’m I right?

Also remember to convert the input. You can convert it directly using
float(input (“type in your rate”))

I´m writing it mainly in my command prompt in VS Code but I transfer it over to the code editor when I think I´ve got it right.

Why do I need to convert the entire input? Why can´t I just enclose the result in a variable and then convert that to float?

You can do it that way, it’s correct,

That’s what it’s required of you to do by your online class editor.

If you run it your own personal editor you won’t get an error. But that’s what by your instructor, so you have to follow the instruction to make the codes work

1 Like

The prompt you actually get back telling to read rate from input is not from python, python don’t give such error. It’s from your online class

1 Like

I´m having a similar problem with a problem that is practically the same with the exception that I must work through a function to solve it. The problem is I am getting a larger amount than I should when I work it out through the command prompt in VS Code.

The code is below.
The test values are 45 hours with a 10.50 rph.
The result should be 498.75; yet, I receive back 551.25.
This makes no sense to me because I´m assembling all the necessary elements to return the correct amount. I think I may have a mathematical error. But not sure where. I checked the math and everything seems to work out. So why am I getting back a larger amount?

#Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours.

#hours worked and rate per hour
hrs = float(input("Write the hours:"))
rph = float(input("Write the rate:"))
#regular pay
reg = hrs*rph
#overtime hours
oth = hrs - 40 
#rate while working overtime hours
otr = rph*1.5
overtime = oth*otr
def computepay(hrs, rph):
    if hrs <= 40:
        pay = reg
        pay = float(overtime + reg)
    return pay 
print("Pay", computepay(hrs, rph))

Python is correct,
Base on your code and functionality, python is printing the accurate output.

Looking at your function, then the if statement, since 45 is not less than or equal to 40, the code in the if block doesn’t run.

Then python moves to the code in else block and run d code there. Pay attention to the code in the else block and try and make the calculation yourself. You will notice that python is correct and just doing what you tell it to do.

Since you are expecting a different output, it means you have not well implement your logic and calculation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.