创建一个小游戏。为什么player变量不起作用?

2024-10-01 05:00:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python创建一个小游戏。这个游戏的目的是把所有的牌配对。我的原始代码有两个子例程,它们有很多重复的代码(找到here)。我想把它们浓缩起来,但没用。你知道吗

以下是当前代码:

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,依此类推,这样程序的其余部分就知道该把这一点归功于谁了。你知道吗

为什么这样不行?你知道吗


Tags: to代码fromyouinputsideplayerpick
1条回答
网友
1楼 · 发布于 2024-10-01 05:00:02

请尝试以下代码:

__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的内容,那么程序就死了。你知道吗

相关问题 更多 >