Python 代码运行两次
Python code runs two times
我正在尝试在 python 中制作剪刀石头布游戏,同时尝试实施胜利/失败的积分系统。当我 运行 代码时,我被要求选择石头布或剪刀,我选择了我的答案,但出于某种原因我再次被问到,有人能帮我吗? (我几乎是初学者)
from random import randint
def RPS():
UserPts = 0
AIPts = 0
def Game():
moves = ["Rock","Paper","Scissors"]
def genAImove():
genai = moves[randint(0,2)]
return genai
genAImove()
AImove = genAImove()
def genUserMove():
genu = raw_input("Choose your move ('Rock','Paper' or 'Scissors')\n")
return genu
genUserMove()
UserMove = genUserMove()
if UserMove == "Rock" and AImove == "Rock":
print "The AI chose rock too.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Rock" and AImove == "Paper":
print "The AI chose paper.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Rock" and AImove == "Scissors":
print "The AI chose scissors.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Rock":
print "The AI chose rock.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Paper":
print "The AI chose paper.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Scissors":
print "The AI chose scissors.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Rock":
print "The AI chose rock.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Paper":
print "The AI chose paper.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Scissors":
print "The AI chose scissors.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
Game()
RPS()
在这些行中:
genUserMove()
UserMove = genUserMove()
您先调用 genUserMove
,它询问您,然后 再次调用它,并分配结果。
只需删除第一行。
我正在尝试在 python 中制作剪刀石头布游戏,同时尝试实施胜利/失败的积分系统。当我 运行 代码时,我被要求选择石头布或剪刀,我选择了我的答案,但出于某种原因我再次被问到,有人能帮我吗? (我几乎是初学者)
from random import randint
def RPS():
UserPts = 0
AIPts = 0
def Game():
moves = ["Rock","Paper","Scissors"]
def genAImove():
genai = moves[randint(0,2)]
return genai
genAImove()
AImove = genAImove()
def genUserMove():
genu = raw_input("Choose your move ('Rock','Paper' or 'Scissors')\n")
return genu
genUserMove()
UserMove = genUserMove()
if UserMove == "Rock" and AImove == "Rock":
print "The AI chose rock too.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Rock" and AImove == "Paper":
print "The AI chose paper.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Rock" and AImove == "Scissors":
print "The AI chose scissors.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Rock":
print "The AI chose rock.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Paper":
print "The AI chose paper.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Paper" and AImove == "Scissors":
print "The AI chose scissors.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Rock":
print "The AI chose rock.\nLoss."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Paper":
print "The AI chose paper.\nWin."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
if UserMove == "Scissors" and AImove == "Scissors":
print "The AI chose scissors.\nDraw."
def cont():
cnt = raw_input("Do you want to continue? (Y\N)")
if cnt == "Y":
Game()
elif cnt == "N":
exit
else:
print "Pick Y or N"
cont()
cont()
Game()
RPS()
在这些行中:
genUserMove()
UserMove = genUserMove()
您先调用 genUserMove
,它询问您,然后 再次调用它,并分配结果。
只需删除第一行。