154k views
1 vote
Please hand in your source code and runs to test valid and invalid data. Write a program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item

1 Answer

2 votes

Final answer:

Python Code Template:

```python

def calculate_retail_price():

try:

# Get input from the user

wholesale_cost = float(input("Enter the item's wholesale cost: $"))

markup_percentage = float(input("Enter the markup percentage: "))

# Validate input

if wholesale_cost < 0 or markup_percentage < 0:

raise ValueError("Input values must be non-negative.")

# Calculate retail price

markup_amount = wholesale_cost * (markup_percentage / 100)

retail_price = wholesale_cost + markup_amount

# Display the result

print(f"The retail price of the item is: ${retail_price:.2f}")

except ValueError as e:

print(f"Error: {e}")

# Call the function to execute the program

calculate_retail_price()

```

Step-by-step explanation:

This Python program prompts the user for the wholesale cost and markup percentage of an item. It then calculates the retail price using the formula: `Retail Price = Wholesale Cost + Markup Amount`. The program also includes input validation to ensure that both the wholesale cost and markup percentage are non-negative.

If the user enters invalid data, the program catches the `ValueError` and prints an error message. The final retail price is displayed with a precision of two decimal places. The code structure promotes readability and modularization, making it easy to understand and maintain.

User Maarten Ter Horst
by
8.1k points