我正在用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,依此类推,这样程序的其余部分就知道该把这一点归功于谁了。你知道吗
为什么这样不行?你知道吗
请尝试以下代码:
还有几个问题需要你解决: 1您需要输入的数字是0-8,而不是1-9 2如果用户在没有任何输入的情况下点击enter,或者输入了无法转换为int的内容,那么程序就死了。你知道吗
相关问题 更多 >
编程相关推荐