If Statements
If statements are used to execute a block of code only if a certain condition is met. Check and understand the following example, then tackle the problems that follow.
Problem 1
Section titled “Problem 1”Decide on an integer to serve as the “magic number.” This can be any integer of your choice.
Write a program that prompts the user to guess the magic number.
- If the user’s guess matches the magic number, print:
Good guess! - If the guess is incorrect, print:
Sorry, but no :(
Assume the user will always input an integer value.
Problem 2
Section titled “Problem 2”Write a program that prompts the user for a tax filing code (0 → “Single filer”, 1 → “Married, Filing Jointly.”) and an annual salary, and outputs the tax percentage based on their tax bracket from the following table. Note that this is a simplified tax calculation for educational purposes.
| Tax Percentage | Single Filers | Married, Filing Jointly |
|---|---|---|
| 10% | [$0, $9,875) | [$0, $19,750) |
| 12% | [$9,875, $40,125) | [$19,750, $80,250) |
| 22% | [$40,125 - $85,525) | [$80,250 - $171,050) |
| 24% | [$85,525 - ∞) | [$171,050 - ∞) |
Assume the user will always enter valid inputs for the tax filing code and salary.
For example, if the user enters 0 for the tax filing code (Single filer) and 50000 for the salary, the program should print: 22%
Problem 3
Section titled “Problem 3”Write a program that prompts the user for their job title code (0 → “Mechanical Engineer”, 1 → “Software Engineer”, 2 → “Electrical Engineer”, 3 → “Architectural Engineer”) then for their state code (0 → “Oregon”, 1 → “California”).
Ensure the prompts clearly explain what each code represents. If the user enters an invalid code, print an appropriate error message and terminate the program. You can assume the user will enter an integer for both the job title code and state code.
If both codes are valid, print the expected salary based on the following table.
| Job Title | Oregon | California |
|---|---|---|
| Mechanical Engineer | $88,830 | $104,203 |
| Software Engineer | $112,197 | $140,244 |
| Electrical Engineer | $97,355 | $110,740 |
| Architectural Engineer | $102,094 | $123,729 |
For example, if the user enters 0 for the job title (Mechanical Engineer) and 1 for the state (California), the program should print: $104,203