Lab 3: Pay Calculator¶

In this lab, you'll write a program that takes input from the user, performs a computation, and then displays the results.

Exercise 1: Run this line of code and think about what it is doing

fav_color = input("What is your favorite color? ")
  • Why does the program pause when it runs this line of code?
  • This is an assignment statement. How is it like other assignment statements you've seen?
  • What variable is being assigned a value?
  • What value is saved to that variable?
  • What type does this variable have?
  • Write the line of code that would diplay this value to the user.
  • Write the line of code that would diplay the type of fav_color.

Exercise 2: Let's say we want to ask the user what their favorite number is. Each of the three lines might work. Which do you think is the most appropriate?

fav_number = input("What is your favorite number? ")
fav_number = int( input("What is your favorite number? ") )
fav_number = float( input("What is your favorite number? ") )
  • What kind of program might need the int() version?
  • What does int() do here?

Exercise 3: The following code will run, but it probably won't give the result that the programmer was hoping for. Go ahead and run it.

first_number = input("What is your favorite number? ")
second_number = input("What is your second favorite number? ")
total = first_number + second_number
print("The sum of your two favorite numbers is",total)

In Thonny, you probably want to write this as a script, save it as a .py file, and then run it. The steps for doing this are as follows:

  1. Click the New button to open a new tab.
  2. Type (or paste) your code into the editor.
  3. Click the Save button and save the file to a convenient location on your computer.
  4. Click the Stop/Restart button to reset the shell environment (and clear out any previous variables from other runs).
  5. Click the Run button to run your script.
  6. Interact with your program in the Shell window.

  • Why is the result not as expected? When a program gives the wrong result but doesn't trigger a Python error like a SyntaxError or TypeError, it's called a logic error.
  • Fix this program so that it gives the expected result.

Challenge Exercise 4: Write a program that asks the user to enter their hourly wage and their number of hours worked, and then display their total pay for that pay period. This is what it should look like:

When you are finished, upload your .py file to the codePost Lab 3: Pay Calculator assignment. For this assignment, codePost will automatically test your code on several different inputs and check for the correct results, so make sure you test your program with additional test cases to ensure it is working.