Asked by nick
chips_price = 1.50
soda_price = 1.25
candy_price = 0.75
TAX_RATE = 0.06 # 6%
chips_qty = get_int("How many chips would you like? ")
soda_qty = get_int("How many sodas would you like? ")
candy_qty = get_int("How many candies would you like? ")
subtotal = (chips_qty * chips_price) + (soda_qty * soda_price) + (candy_qty * candy_price)
tax = subtotal * TAX_RATE
total = subtotal + tax
print(f"\nTotal due: ${total:.2f} (including ${tax:.2f} tax)")
payment = get_float("Enter payment amount: $")
while payment < total:
print(f"Insufficient payment. You still owe ${total - payment:.2f}.")
more = get_float("Enter additional payment amount: $")
payment += more
change = payment - total
print("\n--- Snack Shop Receipt ---")
print(f"Chips ({chips_qty} x ${chips_price:.2f}): ${chips_qty * chips_price:.2f}")
print(f"Soda ({soda_qty} x ${soda_price:.2f}): ${soda_qty * soda_price:.2f}")
print(f"Candy ({candy_qty} x ${candy_price:.2f}): ${candy_qty * candy_price:.2f}")
print(f"Subtotal: ${subtotal:.2f}")
print(f"Tax (6%): ${tax:.2f}")
print(f"Total: ${total:.2f}")
print(f"Payment: ${payment:.2f}")
print(f"Change: ${change:.2f}")
print("--------------------------")
print("Thank you for shopping at the Snack Shop!")
if __name__ == "__main__":
main()
```
All Answers 1
Answered by
GPT-5 mini
AI
I fixed the syntax/logic problems and added simple input-validation helpers so the script runs standalone. Main fixes:
- Added a main() function and the if __name__ guard.
- Fixed the indentation on the while loop.
- Implemented get_int/get_float to validate input.
- Rounded tax and totals to cents to avoid floating-point cent issues.
Corrected script:
```python
def get_int(prompt):
while True:
try:
value = int(input(prompt))
if value < 0:
print("Please enter a non-negative integer.")
continue
return value
except ValueError:
print("Invalid input. Please enter an integer.")
def get_float(prompt):
while True:
try:
value = float(input(prompt))
if value < 0:
print("Please enter a non-negative amount.")
continue
return value
except ValueError:
print("Invalid input. Please enter a number.")
def main():
chips_price = 1.50
soda_price = 1.25
candy_price = 0.75
TAX_RATE = 0.06 # 6%
chips_qty = get_int("How many chips would you like? ")
soda_qty = get_int("How many sodas would you like? ")
candy_qty = get_int("How many candies would you like? ")
subtotal = (chips_qty * chips_price) + (soda_qty * soda_price) + (candy_qty * candy_price)
tax = round(subtotal * TAX_RATE, 2)
total = round(subtotal + tax, 2)
print(f"\nTotal due: ${total:.2f} (including ${tax:.2f} tax)")
payment = get_float("Enter payment amount: $")
while payment < total:
print(f"Insufficient payment. You still owe ${total - payment:.2f}.")
more = get_float("Enter additional payment amount: $")
payment += more
change = round(payment - total, 2)
print("\n--- Snack Shop Receipt ---")
print(f"Chips ({chips_qty} x ${chips_price:.2f}): ${chips_qty * chips_price:.2f}")
print(f"Soda ({soda_qty} x ${soda_price:.2f}): ${soda_qty * soda_price:.2f}")
print(f"Candy ({candy_qty} x ${candy_price:.2f}): ${candy_qty * candy_price:.2f}")
print(f"Subtotal: ${subtotal:.2f}")
print(f"Tax (6%): ${tax:.2f}")
print(f"Total: ${total:.2f}")
print(f"Payment: ${payment:.2f}")
print(f"Change: ${change:.2f}")
print("--------------------------")
print("Thank you for shopping at the Snack Shop!")
if __name__ == "__main__":
main()
```
If you want, I can also:
- Add formatted currency functions,
- Accept dollar amounts like "$5.00" in input,
- Handle exact change breakdown (bills/coins).
- Added a main() function and the if __name__ guard.
- Fixed the indentation on the while loop.
- Implemented get_int/get_float to validate input.
- Rounded tax and totals to cents to avoid floating-point cent issues.
Corrected script:
```python
def get_int(prompt):
while True:
try:
value = int(input(prompt))
if value < 0:
print("Please enter a non-negative integer.")
continue
return value
except ValueError:
print("Invalid input. Please enter an integer.")
def get_float(prompt):
while True:
try:
value = float(input(prompt))
if value < 0:
print("Please enter a non-negative amount.")
continue
return value
except ValueError:
print("Invalid input. Please enter a number.")
def main():
chips_price = 1.50
soda_price = 1.25
candy_price = 0.75
TAX_RATE = 0.06 # 6%
chips_qty = get_int("How many chips would you like? ")
soda_qty = get_int("How many sodas would you like? ")
candy_qty = get_int("How many candies would you like? ")
subtotal = (chips_qty * chips_price) + (soda_qty * soda_price) + (candy_qty * candy_price)
tax = round(subtotal * TAX_RATE, 2)
total = round(subtotal + tax, 2)
print(f"\nTotal due: ${total:.2f} (including ${tax:.2f} tax)")
payment = get_float("Enter payment amount: $")
while payment < total:
print(f"Insufficient payment. You still owe ${total - payment:.2f}.")
more = get_float("Enter additional payment amount: $")
payment += more
change = round(payment - total, 2)
print("\n--- Snack Shop Receipt ---")
print(f"Chips ({chips_qty} x ${chips_price:.2f}): ${chips_qty * chips_price:.2f}")
print(f"Soda ({soda_qty} x ${soda_price:.2f}): ${soda_qty * soda_price:.2f}")
print(f"Candy ({candy_qty} x ${candy_price:.2f}): ${candy_qty * candy_price:.2f}")
print(f"Subtotal: ${subtotal:.2f}")
print(f"Tax (6%): ${tax:.2f}")
print(f"Total: ${total:.2f}")
print(f"Payment: ${payment:.2f}")
print(f"Change: ${change:.2f}")
print("--------------------------")
print("Thank you for shopping at the Snack Shop!")
if __name__ == "__main__":
main()
```
If you want, I can also:
- Add formatted currency functions,
- Accept dollar amounts like "$5.00" in input,
- Handle exact change breakdown (bills/coins).
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.