Home > Software engineering >  How to Check if Costs go Below Zero in Python
How to Check if Costs go Below Zero in Python

Time:11-05

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.

CodePudding user response:

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")

CodePudding user response:

EDIT

Based on @Tim's feedback that this is not a good use of recursion, I've updated my code, using the same basic idea.


The problem is that in your input, you are comparing the quantity of items against the balance of points, rather than comparing the cost of how many they want to what is available.

In order to minimize your lines of code, you can also create a function with recursion to keep asking for input while the number is invalid. I'd suggest the following:

balance = 100

prices  = {'glowsticks':10, #etc}

def check_balance(item, qty):
    global balance 
    while (cost := prices[item] * qty) > balance:
        qty = int(input("Too many, try again: "))
    else:
        balance -= cost
        print(f"New balance is {balance}.")
        return 

CodePudding user response:

Let's say n is the number of wooden snakes you want to buy

You're checking if the balance is less than n (so if I want 2 snakes, as long as the balance is greater than of equal to 2, it will let me buy the wooden snakes) but you need to check if the cost of buying n snakes is less than the balance.

This of course applies to all items in your program.

You can also replace your elif statements with else statements as if the cost is not lower or equal to the balance, you know for sure without checking that it's going to be greater.

I'll use wooden snakes as an example

replace

    #How many wooden snakes the player wants
    prize1 = int(input("Tell me again how many wooden snakes you want "))
    if prize1 <= balance:
        #buy the item
    elif prize1 > balance:
        print("Too many wooden snakes, please change your amount")
        break

with

    #How many wooden snakes the player wants
    prize1 = int(input("Tell me again how many wooden snakes you want "))
    if prize1 * 35 <= balance:
        #buy the items
    else:
        print("Too many wooden snakes, please change your amount")
  • Related