Python 创建一个小游戏。为什么';播放器变量不能工作吗?

Python 创建一个小游戏。为什么';播放器变量不能工作吗?,python,python-3.x,Python,Python 3.x,我正在用Python创建一个小游戏。游戏的目的是匹配所有的牌。我的原始代码有两个子例程,其中包含大量重复代码(已找到)。我试图把它们浓缩,但没用 以下是当前代码: def pick(Player): P1score = 0 P2score = 0 Player = 0 if Player == 0: Player = Player+1 elif Player == 1: Player = 2 elif Player

我正在用Python创建一个小游戏。游戏的目的是匹配所有的牌。我的原始代码有两个子例程,其中包含大量重复代码(已找到)。我试图把它们浓缩,但没用

以下是当前代码:

def pick(Player):
    P1score = 0
    P2score = 0
    Player = 0
    if Player == 0:
        Player  = Player+1
    elif Player == 1:
        Player = 2
    elif Player == 2:
        Player = 1
    if len(set1) == 0:
        won(P1score,P2score)
    else:
        print("It's Player" +str(Player) + "'s turn and there are still "+ str(len(set1)) + " pairs left to find!")
        side1 =int(input("What number would you like to pick from side one?"))
        side2 =int(input("What number would you like to pick from side two?"))
        #This function makes the player input 2 valid card numbers
        while side1 not in range(len(set1)) or side2 not in range(len(set2)):
            side1 =int(input("What number would you like to pick from side one?"))
            side2 =int(input("What number would you like to pick from side two?"))
        s1pick = set1[side1]
        s2pick = set2[side2]
        matching(s1pick, s2pick, set1, set2,P1score, P2score)

def matching(s1pick,s2pick,set1,set2,P1score,P2score,Player):
    picks = []
    picks.append(s1pick)
    picks.append(s2pick)
    if picks[0] == picks[1]:
        if Player == 1:
            P1score = P1score+1
        elif Player == 2:
            P2score = P2score + 1
        for i in range (10):
            print("Correct")
            time.sleep(0.2)
        print("Good Job, you successfully picked " + picks[0] + " and "+ picks[0])
        set1.remove(s1pick)
        set2.remove(s2pick)
        pick(Player)
    else:
        print("Sorry, not a match! From side one you picked "+ picks[0] + " and from side 2 you picked " + picks[1])
        pick(Player)

Player = 0
pick(Player)
上面还有更多的代码,但这并不影响这一部分

基本上,我希望它将变量Player从1改为2,然后再改回1,依此类推,这样程序的其余部分就知道该将积分归谁


为什么这不起作用?

请尝试以下代码:

__author__ = "mexO"
# SnapGameForFun
import random
from random import shuffle
import time

cards = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]
print("Here are all the cards there could be, try to find out where they could be hiding!")
time.sleep(3)
for i in cards:
    print(i)
    time.sleep(0.5)

print("The cards have been numbered 1 to 9.\nPick a number from the first list and one from the second list."
      "\nIf they match the cards will be removed from the list.\nEasy right?\n\n\n\nLets start!")

set1 = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]
set2 = ["Horse", "Cow", "Pig", "Goat", "Chicken", "Sheep", "Donkey", "Duck", "Rabbit"]

random.shuffle(set1,random.random)
random.shuffle(set2,random.random)

P1score = 0
P2score = 0
#--------------------------WON--------------------------
#--------------------------WON--------------------------
#--------------------------WON--------------------------

def won(P1score,P2score):
    if P1score > P2score:
        winner = "Player 1"
        winnerscore = P1score
    else:
        winner = "Player 2"
        winnerscore = P2score
    print("Well done to {0} who got {1} of the 10 pairs correct!".format(winner, winnerscore))

def pick(player=0):
    player = player % 2
    global P1score, P2score
    if len(set1) == 0:
        won(P1score,P2score)
    else:
        print("It's Player {0}'s turn and there are still {1} pairs left to find!".format(player + 1, str(len(set1))))
        side1 =int(input("What number would you like to pick from side one?"))
        side2 =int(input("What number would you like to pick from side two?"))
        while side1 not in range(len(set1)) or side2 not in range(len(set2)):
            side1 =int(input("What number would you like to pick from side one?"))
            side2 =int(input("What number would you like to pick from side two?"))
        s1pick = set1[side1]
        s2pick = set2[side2]
        if player == 0:
            matching(s1pick, s2pick, set1, set2, P1score, player)
        else:
            matching(s1pick, s2pick, set1, set2, P2score, player)

def matching(s1pick,s2pick,set1,set2,score,player):
    global P1score, P2score
    if s1pick == s2pick:
        if player == 0:
            P1score += 1
        else:
            P2score += 1
        print('P1score:', P1score, 'P2score', P2score)
        for i in range (10):
            print("Correct")
            time.sleep(0.2)
        print("Good Job, you successfully picked {0} and {1}\n".format(s1pick, s2pick))
        set1.remove(s1pick)
        set2.remove(s2pick)
        pick(player + 1)
    else:
        print("Sorry, not a match! From side one you picked {0} and from side 2 you picked {1}\n".format(s1pick, s2pick))
        print('P1score:', P1score, 'P2score', P2score)
        pick(player+1)


if __name__ == "__main__":
    pick()
还有几个问题需要您解决: 1.您需要输入的数字是0-8,而不是1-9
2.如果用户只是在没有任何输入的情况下按enter键,或者输入了无法转换为int的内容,则程序将终止。

我没有阅读您的全部代码,但
Player=0
可能不是您希望执行的操作。这意味着,无论使用什么值调用
pick
,它都将立即被丢弃,并使用零。一般来说,不先查看函数的参数就无条件覆盖函数的参数是个坏主意。我相信你的间距是不正确的。看起来你的缩进被弄坏了。我试着把它修好,但仔细检查一下,以防把它弄糟。特别是最后两行:我想这些不是为了缩进。