Skip to content

Instantly share code, notes, and snippets.

@Sh-ui
Created January 30, 2026 17:47
Show Gist options
  • Select an option

  • Save Sh-ui/a06f14c090844e67638960c540102173 to your computer and use it in GitHub Desktop.

Select an option

Save Sh-ui/a06f14c090844e67638960c540102173 to your computer and use it in GitHub Desktop.
learn python basics by using constants, types, input, and if/else; to build a weekly pay calculator

Python Basics: Building an Overtime Pay Calculator

The Problem

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.)


Following Along

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.


Your Debugging Tool (Use This From the Start)

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.


Step 1: Breaking Down the Problem

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.


Understanding Data Types

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.


Type Conversion: Why It Matters

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.


Step 2: Getting User Input

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.


Step 3: Thinking Through the Logic

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.


Step 4: Putting It All Together with Conditional Logic

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("__")))

Optional: Combining Steps

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.


Key Takeaways

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment