This project is a simple Python program that calculates the total annual bonus based on a user's monthly salary and bonus percentage. The program ensures input validation and prevents common errors that could lead to incorrect calculations or crashes.
- Greets the user and requests their name.
- Prompts the user to input their salary and bonus percentage.
- Validates input to prevent errors (e.g., negative values, non-numeric input).
- Calculates the annual bonus, including an additional fixed amount of R$ 1000.
- Displays the correctly formatted result.
# 1. Greet the user and request name input
name = input("Hello! Enter your name: ").strip()
# 2. Function to validate and obtain a positive numeric value
def get_value(message):
while True:
try:
value = float(input(message).strip().replace(",", ".")) # Supports comma as decimal separator
if value < 0:
print("Error: The value cannot be negative. Please try again.")
continue
return value
except ValueError:
print("Error: Please enter a valid number.")
# 3. Request salary and bonus (percentage)
salary = get_value(f"Welcome, {name}! Enter your monthly salary: ")
bonus_percentage = get_value(f"{name}, enter your bonus percentage (e.g., 10 for 10%): ")
# 4. Calculate the total annual bonus (including 1000 fixed additional)
bonus_total = (salary * (1 + (bonus_percentage / 100))) + 1000
# 5. Display the correctly formatted result
print(f"{name}, your total annual bonus is: R$ {bonus_total:.2f}")Below is a list of potential issues in the previous version of the code and how they were resolved.
| π Bug | π₯ Cause | π§ Solution |
|---|---|---|
| 1οΈβ£ Invalid Input | The user may enter a non-numeric value (e.g., "five thousand") for salary or bonus, causing a ValueError. |
Use try-except to catch errors and prevent crashes. |
| 2οΈβ£ Negative Values | If salary or bonus is negative, the calculation may produce incorrect results. |
Ensure values are positive before processing. |
| 3οΈβ£ Empty Input | Pressing Enter without typing anything causes an error when converting "" to float. |
Check if input is empty before converting it. |
| 4οΈβ£ Extra Spaces in Input | Input with leading/trailing spaces may cause unexpected behavior. | The .strip() function ensures clean input. |
| 5οΈβ£ Formatting Issues | Printing total directly may display unformatted numbers. |
Use :.2f formatting to ensure two decimal places. |
| 6οΈβ£ Incorrect Bonus Calculation | The previous code assumed the bonus was a direct multiplier instead of a percentage. | Clarify input format (1.5 = 150%) and correctly apply the percentage. |
βοΈ Better Input Validation β Prevents crashes and incorrect calculations.
βοΈ Improved User Experience β Clearer instructions and better formatting.
βοΈ More Robust Calculations β Ensures bonus is applied correctly.
To run this program, make sure you have Python 3.x installed, then execute:
python main.pyπ€ Luiz Otavio Delgado
π§ Contact: luizotavio.delgado@gmail.com
π GitHub: https://github.com/luizodelgado