i=1
玩另一个游戏或退出游戏
def done():
quitvalue=str(input("Play Again?(y/n):"))
if quitvalue=='n' and i==0:
SystemExit
elif quitvalue=="y":
i=1
#Loop to Restart Game
def rps(play1,play2):
#Check Winner of Game
if play1==play2:
print("It's a tie!")
done()
elif play1=='r':
if play2=='s':
print(play1name+" Wins!")
done()
else:
print(play2name+" Wins!")
done()
elif play1=='p':
if play2=='r':
print(play1name+" Wins!")
done()
else:
print(play2name+" Wins!")
done()
elif play1=='s':
if play2=='p':
print(play1name+" Wins!")
done()
else:
print(play2name+" Wins!")
done()
#Player Input
while i==1:
play1name=str(input("Player 1 Name?:"))
play2name=str(input("Player 2 Name?:"))
play1= str(input(play1name+" Choose Rock(r),Paper(p), Scissors(s):"))
play2= str(input(play2name+" Choose Rock(r),Paper(p), Scissors(s):"))
i=0
rps(play1,play2)
Tags:
你想在
done()
函数中raise SystemExit
退出吗?你知道吗如果是这样的话,就不需要跟踪命名含糊不清的变量
i
并处理范围界定问题。见here for some useful explanation。你知道吗您还应该将玩家名称输入移出游戏循环,
input()
将返回一个字符串。你知道吗我已经整理了代码并删除了一些不必要的重复部分。你知道吗
您可能还需要检查
rps()
函数中的有效移动。你知道吗这里的问题是
done()
函数中的i
引用了deafult在函数中本地声明的变量i
,而不是在程序开始时声明的全局变量i
。要解决此问题,请在引用i
之前在done()
函数中添加global i
:相关问题 更多 >
编程相关推荐