Is_this_my_username
Is_this_my_username

Reputation: 176

Value Error in Python where the code fails at certain places

I was making a small (fake money) betting game to play with my friends during this cricket season. Below is the "prototype" of the application that I will create. While I am running it, some weird problems occur. I am attaching my code as well as the error that I am getting

    initMoney = "100 100 100 100"
    betWinners = []
    listWinner = []
    first = initMoney[1]
    second = initMoney[2]
    third = initMoney[3]
    fourth = initMoney[4]
    class Player:
        def __init__(self, name):
            self.name = name
        def money(self, cash):
            self.cash = cash
            return self.cash
        def bet(self, amount):
            self.amount = amount
            return self.amount 
    playerA = Player("A")
    playerB = Player("B")
    playerC = Player("C")
    playerD = Player("D")      
    players = [playerA, playerB, playerC, playerD]
    
    playerA.money(initMoney[1])
    playerB.money(initMoney[2])
    playerC.money(initMoney[3])
    playerD.money(initMoney[4])
    def bet():
        winner1 = (str(input("Who do you want to be the winner? "))).upper()
        winner2 = (str(input("Who do you want to be the winner? "))).upper()
        winner3 = (str(input("Who do you want to be the winner? "))).upper()
        winner4 = (str(input("Who do you want to be the winner? "))).upper()
        listWinner = [winner1, winner2, winner3, winner4]
    
        bet1 = int(input("How much do you bet? "))
        bet2 = int(input("How much do you bet? "))
        bet3 = int(input("How much do you bet? "))
        bet4 = int(input("How much do you bet? "))
        playerA.bet(bet1)
        playerB.bet(bet2)
        playerC.bet(bet3)
        playerD.bet(bet4)
        if (bet1 >= int(playerA.cash)):
            print ("Player 1 is bankcrupt")
            bet1 = int(input("How much do you bet?"))
        if (bet2 >= int(playerB.cash)):
            print ("Player 2 is bankcrupt")
            bet2 = int(input("How much do you bet?"))
        if (bet3 >= int(playerC.cash)):
            print ("Player 3 is bankcrupt")
            bet3 = int(input("How much do you bet?"))
        if (bet4 >= int(playerD.cash)):
            print ("Player 4 is bankcrupt")
            bet4 = int(input("How much do you bet?"))
        global pot
        pot = bet1 + bet2 + bet3 + bet4
        return pot, listWinner, playerA.bet(bet1), playerB.bet(bet2), playerC.bet(bet3), playerD.bet(bet4)
    def winner():
        winner = (str(input("Who won? "))).upper()
        for i in listWinner:
            if i == winner:
                betWinners.append(i)
        dividedSum = pot/(len(betWinners))
        for person in players:
            if person in betWinners:
                person.cash += dividedSum
            else:
                person.cash -=  person.amount
    
    bet()
    winner()

The error/output that I recieve

Who do you want to be the winner? csk\
Who do you want to be the winner? csk
Who do you want to be the winner? c
Who do you want to be the winner? s
How much do you bet? 100
How much do you bet? 100
How much do you bet? 100
How much do you bet? 100
Player 1 is bankcrupt
How much do you bet?0
Player 2 is bankcrupt
How much do you bet?0
Traceback (most recent call last):
  File "c:/Users/Shubhkarman/Desktop/Codeolgy/Python/Cricket Betting/Prototype.py", line 70, in <module>
    bet()
  File "c:/Users/Shubhkarman/Desktop/Codeolgy/Python/Cricket Betting/Prototype.py", line 49, in bet
    if (bet3 >= int(playerC.cash)):
ValueError: invalid literal for int() with base 10: ' '

The point I am trying to make is that, for the first two players the all the code works, but suddenly it fails, on the third one. Why is it so?

Upvotes: 0

Views: 49

Answers (1)

Unknown artist
Unknown artist

Reputation: 947

You are indexing a string variable initMoney to get your starting money values (and also, you're indexing from 1, not zero, but that's a separate issue).

So initMoney[1] ends up being "0" (2nd character of initMoney), all the way to initMoney[4] which ends up being " ".

Upvotes: 2

Related Questions