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)
btnName.sizeToFit()
答案 0 :(得分:2)
这里的问题是i
函数中的done()
引用了一个变量i
,该函数由deafult在函数中本地声明,而不是全局变量{{1}在程序开头声明。要解决此问题,请在i
函数中添加global i
,然后再引用done()
:
i
答案 1 :(得分:0)
您的意思是raise SystemExit
函数中的done()
退出吗?
如果是这样,您不需要跟踪模糊命名变量i
并处理范围问题。请参阅here for some useful explanation。
您还应该将游戏名称输入移出游戏循环,input()
将返回一个字符串。
我整理了代码并删除了一些不必要的重复部分。
def done():
"""Prompt user to continue or exit"""
quit_value = input("Play Again?(y/n): ").lower()
if quit_value == 'n':
raise SystemExit
def rps(play1, play2):
"""Play a single round of Rock, Paper, Scissors"""
# Check Winner of Game
if play1 == play2:
print("It's a tie!")
elif play1 == 'r':
if play2 == 's':
print(play1name + " Wins!")
else:
print(play2name + " Wins!")
elif play1 == 'p':
if play2 == 'r':
print(play1name + " Wins!")
else:
print(play2name + " Wins!")
elif play1 == 's':
if play2 == 'p':
print(play1name + " Wins!")
else:
print(play2name + " Wins!")
# get player names
play1name = input("Player 1 Name?: ")
play2name = input("Player 2 Name?: ")
# Player Input
while True: # done() exits the program, so loop forever
play1 = input(play1name + " Choose Rock(r),Paper(p), Scissors(s): ")
play2 = input(play2name + " Choose Rock(r),Paper(p), Scissors(s): ")
# resolve the plays
rps(play1, play2)
# check for a rematch
done()
您可能还想检查rps()
功能中的有效移动。