I’m very, very new to code and my current class is sort of hitting us with all kinds of information at once. It’s making a bit difficult to latch onto certain concepts. For this particular problem, our instructor wants us to write a code to calculate employee salaries based on hours worked. Here is the prompt: "A company wants a program that will calculate the weekly paycheck for an employee based on how many hours they worked. For this company, an employee earns $20 an hour for the first 40 hours that they work. The employee earns overtime, $30 an hour, for each hour they work above 40 hours."
He also gave us an example to work with: "If an employee works 60 hours in a week, they would earn $20/hr for the first 40 hours. Then they would earn $30/hr for the 20 hours they worked overtime. Therefore, they earned: ($20/hr * 40hrs) + ($30/hr * 20 hrs) = $800 + $600 = $1400 total."
Like I said, we were given a lot of information at once. I’m trying to use an if statement to make the code, but I know very well it’s not written correctly. It’s filled with all kinds of syntax errors. I’m just not sure what to do.
work_hours = str(input('Enter hours worked' ))
if (work_hours <= 40)
salary = 20 * work_hours
if (work_hours > 40)
over_salary = 30 * (work_hours - 40)
reg_salary = 20 * 40 + over_salary
print(reg_salary)
>Solution :
Change that str
to int
and put colons after your if
statements
work_hours = int(input('Enter hours worked: ' ))
if work_hours <= 40:
salary = 20 * work_hours
elif work_hours > 40:
over_salary = 30 * (work_hours - 40)
salary = 20 * 40 + over_salary
print(salary) # If I input 60 I get 1400
You can see that I changed the line work_hours = str(input('Enter hours worked: ' ))
to work_hours = int(input('Enter hours worked: ' ))
. By changing that line I made sure that the input would be interpreted as a number. I also put colons (:
) after your if
statements, which was the only SyntaxError
I could find.