As a culminating assignment in my class, we were tasked on writing a game that is meant to be played in the lines of amusement parks. Once completing the game, we need to give the player a chance to buy a certain amount of 6 different prizes, as long as they have the points to afford them.
In my program, I made it so that if someone orders a certain amount of one prize that goes over their point average, it tells you you don't have enough points to afford it, and you need to retype your answer.
However, I made an error in that the player's balance (sum of all their points) goes below zero even when a number sent into the program is less then said balance. I think this has something to do with the costs of each prize, since one of them, wooden snakes, is worth 35 points, and the same can be said for the others, albeit with different numbers.
balance = 100
#How many glowsticks the player wants
prize1 = int(input("Tell me again how many glowsticks you want "))
if prize1 <= balance:
prize1 = prize1 * 10
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many glowsticks, please change your amount")
break
#Confirm again if that's how many jumbo glasses the player wanted
prize2 = int(input("Tell me again how many jumbo glasses you want "))
if prize2 <= balance:
prize2 = prize2 * 15
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many jumbo glasses, please change your amount.")
break
#How many inflatable hammers the player wants
prize1 = int(input("Tell me again how many inflatable hammers you want "))
if prize1 <= balance:
prize1 = prize1 * 25
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many inflatable hammers, please change your amount")
break
#How many of dinosaur grabbers the player wants
prize2 = int(input("Tell me again how many dinosaur grabbers you want "))
if prize2 <= balance:
prize2 = prize2 * 30
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many dinosaur grabbers, please change your amount.")
break
#How many wooden snakes the player wants
prize1 = int(input("Tell me again how many wooden snakes you want "))
if prize1 <= balance:
prize1 = prize1 * 35
balance = balance - prize1
print("Your balance is now",balance,"points.")
elif prize1 > balance:
print("Too many wooden snakes, please change your amount")
break
#How many foam swords the player wants
prize2 = int(input("Tell me again how many foam swords you want "))
if prize2 <= balance:
prize2 = prize2 * 40
balance = balance - prize2
print("Your balance is now",balance,"points.")
elif prize2 > balance:
print("Too many foam swords, please change your amount.")
break
This is the fraction of my code where the problem happens. I wanna have it so that the player, once winning, enters how many of each prize they want. If they can't afford the amount they want, as in it would result in your balance going below zero, or it being over your balance, the program would ask you change your amount to suit it.
P.S. The reason the balance is 100 in this example is for demonstration purposes. It may not always equal that number, and could be higher or lower depending on how well the player did throughout the game.
There is a key rule in programming called DRY: Don't Repeat Yourself. Notice how this architecture solves the problem with much less code. It makes it easy to add new items, and if you think of a better way to handle things, one fix works for everything:
balance = 100
options = [
('glowsticks', 10),
('jumbo glasses', 15),
('inflatable hammers', 25),
('dinosaur grabbers', 30),
('wooden snakes',35),
('foam swords', 40)
]
for name, cost in options:
while True:
count = int(input(f"Tell me again how many {name} you want? "))
if count * cost <= balance:
balance -= count * cost
print( "Your balance is now", balance, "points.")
break
else:
print(f"Too many {name}, please change your amount")