-1

所以我想弄清楚如何玩简单的战争游戏。而且我的大部分代码已经输入了,我现在只是遇到了一个简单的错误问题,我想通过它。现在我遇到了错误

Traceback (most recent call last):
File "/Users/ryandaly/Desktop/THe GAME OF WAR.py", line 54, in <module>
playerA = deck1.pop(random.choice(deck1))
TypeError: 'str' object cannot be interpreted as an integer

这是我的完整代码

total = {
   'winA':0,  
   'winB':0
}


import random

def shuffleDeck():

    deck = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']*4

    random.shuffle(deck)        # Now shuffle the deck

    return deck

def dealDecks(deck):
    global deck1
    global deck2
    deck1 = deck[:26]
    deck2 = deck[26:]


def total(hand):

    values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10,
              'J':11, 'Q':12, 'K':13,'A':14}


def war(playerA, playerB):        
    if playerA==playerB:
        print("Tie")
    elif playerA > playerB:
        print("Winner:Player A")
        return 1
    else:        
        print("Winner:player B")
        return -1

def process_game(playerA,playerB):
    result = game(p1c,p2c)   
    if result == -1:
        total['winB'] += 1
    else:
        total['winA'] += 1

deck = shuffleDeck()

dealDecks(deck)

gameplay = input("Ready to play a round: ")

while gameplay == 'y':
    playerA = deck1.pop(random.choice(deck1))
    playerB = deck2.pop(random.choice(deck2))
    print("Player A: {}. \nPlayer B: {}. \n".format(playerA,playerB))
    gameplay = input("Ready to play a round: ")


if total['winA'] > total['winB']:
    print("PlayerA won overall with a total of {} wins".format(total['winA']))
else:
    print("PlayerB won overall with a total of {} wins".format(total['winB']))
4

1 回答 1

1

当你这样做时:

def dealDecks(deck):
    deck1 = deck[:26]
    deck2 = deck[26:]

而调用函数,deck1只会deck2存在于dealDecks作用域内,所以不能在函数外访问。

你可以:

  1. 返回两个列表并在其他地方访问它们

  2. 全球化列表,以便您可以在任何地方访问它们

要使它们全球化,请执行以下操作:

def dealDecks(deck):
    global deck1
    global deck2
    deck1 = deck[:26]
    deck2 = deck[26:]
于 2013-11-06T04:32:38.027 回答