Here's the assignment we're working with:
Write a Python program that calculates weekly pay. The program should multiply hours worked by hourly pay rate to get the weekly pay. Include overtime pay at 1.5 times the regular rate for any hours worked over 40. Use a float conversion for the hourly pay rate.
(Note: I'm paraphrasing how the original question might have been worded, but the core concepts are the same.)
This tutorial is designed for you to code along, block by block. I recommend using online-python.com or any Python environment you prefer.
Important: Copy and paste each code block as you go, running the code after each step to see it work. Don't skip ahead or try to paste everything at once. We'll learn by doing, one piece at a time.
Before we write any program logic, paste this line to your code editor. Always leave it as the last line of code in your program. As you copy and paste the code blocks from this guide into your program to follow along, running what you have will print all the variables you've created so far and their current values so you can see exactly what's stored at each step:
print("\n".join(f"{k} = {v!r}" for k, v in globals().items() if not k.startswith("__")))Don't worry about understanding what this line or how it works right now. Just leave it at the very bottom of whatever code you're running. Every time you run a block, you'll see a list of your variables and what they equal.
Before writing any logic, let's identify what we know from the problem statement. Start by pulling out the constants, the fixed values that won't change.
"for any hours worked over 40" tells us the standard work week is 40 hours.
"overtime pay rate that is 1.5 times the hourly pay" tells us the overtime multiplier is 1.5.
STANDARD = 40 # standard work week in hours
OT_RATE = 1.5 # overtime multiplier
# I put these in all caps to remember they're CONSTANTS.
# Their values won't be changed by the program.Go ahead and run this code. You should see our debug line print out the values of the constants we just defined.
Before we move on, let's talk about the three basic types you need to know for this problem:
5.5 # this is a float (decimal number) 7 # this is an int (whole number) "3.4" # this is a string (text in quotes)
Most code editors will highlight strings in a different color than numbers. Think of a string as literally a string of characters bound together by quotes on each end. Even if it looks like a number to you, Python just sees it for what it is.
Here are more examples of strings:
"hello" "123" "3.14" "True" "False"
See how they're all in quotes? That's the key. This distinction matters because you can't do math with strings. You have to convert them to numbers first.
You only need to convert types when a type change is actually necessary. Let me show you:
"3" + 4 # ERROR! Can't add string and number int("3") + 4 # Result: 7 (converted string to int first) 3.5 + 4 # Result: 7.5 (both are numbers, Python handles it)
When you get input from a user, Python always stores it as a string. That's why conversion is necessary for calculations.
Now let's get the information we need from the user. We'll do this in two stages to make it clear what's happening.
STANDARD = 40
OT_RATE = 1.5
# First, get the user inputs and store them as strings
user_hours_worked = input("Enter the number of hours worked: ")
user_hourly_pay = input("Enter the hourly pay rate: ")
# Now convert those strings into numbers
hours_worked = int(user_hours_worked)
hourly_pay = float(user_hourly_pay)I'm breaking this into two steps so you can see the process clearly. First we get the input (which comes in as strings), then we convert to the number types we need. Later, I'll show you how these steps can be combined, because programming is modular and stackable.
Run this code and try entering some values. Still nothing will print, but Python is storing your numbers.
Now we need to figure out what calculations to do. Let's think about this in pieces before we combine them.
Case A: If the user worked typical hours (40 or less), we just multiply hours by pay rate.
payday = hours_worked * hourly_pay
Case B: But what if the user worked more than 40 hours? Then we need to calculate overtime.
overtime_hours = hours_worked - STANDARD overtime_pay = overtime_hours * hourly_pay * OT_RATE payday = (STANDARD * hourly_pay) + overtime_pay
I've defined both Cases separately to show you how programmers think: make assumptions, write code for each scenario, then use logic to choose which one to execute.
The beauty of programming is that you can use if statements to handle all scenarios at once. Let's combine everything we've built:
STANDARD = 40
OT_RATE = 1.5
user_hours_worked = input("Enter the number of hours worked: ")
user_hourly_pay = input("Enter the hourly pay rate: ")
hours_worked = int(user_hours_worked)
hourly_pay = float(user_hourly_pay)
if hours_worked > STANDARD:
# Case B: User worked overtime
overtime_hours = hours_worked - STANDARD
overtime_pay = overtime_hours * hourly_pay * OT_RATE
payday = STANDARD * hourly_pay + overtime_pay
else:
# Case A: User worked regular hours
payday = hours_worked * hourly_pay
# I left our same debug line at the bottom of the program to print out the values of the variables we created.
print("\n".join(f"{k} = {v!r}" for k, v in globals().items() if not k.startswith("__")))Remember how I split the input and conversion into separate steps? Here's how you could combine them:
hours_worked = int(input("Enter the number of hours worked: "))
hourly_pay = float(input("Enter the hourly pay rate: "))This nests the conversion inside the input function. Both approaches work, use whichever makes more sense to you. When you're learning, the separate steps are often clearer.
You just learned:
- How to identify constants in a problem statement
- The difference between strings, integers, and floats
- Why and how to convert types
- How to get user input
- How to break down logic into smaller pieces (Cases)
- How to use conditional statements to handle different scenarios
Most importantly, you learned to build a program piece by piece, testing as you go. That's how real programming works.
Keep experimenting with the code. Try different inputs. Break things on purpose and see what error messages you get. That's true even at high levels of programming.