Python中的值错误,代码在某些地方失败

Python中的值错误,代码在某些地方失败,python,Python,在这个板球赛季,我和朋友们玩了一个小型(假币)赌博游戏。下面是我将创建的应用程序的“原型”。当我运行它时,出现了一些奇怪的问题。我正在附加我的代码以及我得到的错误 initMoney = "100 100 100 100" betWinners = [] listWinner = [] first = initMoney[1] second = initMoney[2] third = initMoney[3] four

在这个板球赛季,我和朋友们玩了一个小型(假币)赌博游戏。下面是我将创建的应用程序的“原型”。当我运行它时,出现了一些奇怪的问题。我正在附加我的代码以及我得到的错误

    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()
我收到的错误/输出

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: ' '
你想让谁成为赢家?csk\
你想让谁成为赢家?csk
你想让谁成为赢家?C
你想让谁成为赢家?s
你赌多少?100
你赌多少?100
你赌多少?100
你赌多少?100
玩家1是bankcrupt
你赌多少
玩家2是班克罗普
你赌多少
回溯(最近一次呼叫最后一次):
文件“c:/Users/Shubhkarman/Desktop/Codeolgy/Python/Cricket-Betting/Prototype.py”,第70行,在
bet()
文件“c:/Users/Shubhkarman/Desktop/Codeolgy/Python/Cricket-Betting/Prototype.py”,第49行,在bet中
如果(bet3>=int(playerC.cash)):
ValueError:基数为10的int()的文本无效:“”

我想说的一点是,对于前两个玩家,所有的代码都是有效的,但突然在第三个玩家上失败了。为什么会这样?

您正在索引一个字符串变量
initMoney
,以获取您的起始货币值(而且,您从1开始索引,而不是从零,但这是一个单独的问题)


因此
initMoney[1]
最终成为
“0”
(initMoney
的第二个字符),一直到
initMoney[4]
,它最终成为

您正在索引一个字符串变量
initMoney
,以获取起始货币值(而且,您是从1开始编制索引,而不是从零开始编制索引,但这是一个单独的问题)


因此,
initMoney[1]
最终成为
“0”
(initMoney的第二个字符)
,一直到
initMoney[4]
,最终成为

因为代码尝试将空间转换为整数,这是不可能的。因为代码尝试将空间转换为整数,这是不可能的。正确。最简单的修复方法可能是重写
initMoney=[100100100100]
正确。最简单的修复方法可能是重写
initMoney=[100100100100]