【发布时间】:2023-08-26 16:06:01
【问题描述】:
我正在为学校编写基本的剪刀石头布代码,但我的 elif 语句没有运行。
def player1(x):
while x != 'rock' and x != 'paper' and x != 'scissors':
print("This is not a valid object selection")
x = input("Player 1? ")
def player2(x):
while x != 'rock' and x != 'paper' and x != 'scissors':
print("This is not a valid object selection")
x = input("Player 2? ")
def winner():
player1(input("Player 1? "))
player2(input("Player 2? "))
if player1 == 'rock' and player2 == 'rock':
print('Tie')
elif player1 == 'paper' and player2 == 'paper':
print('Tie')
elif player1 == 'rock' and player2 == 'paper':
print('Player 2 wins')
elif player1 == 'paper' and player2 == 'rock':
print('Player 1 wins')
elif player1 == 'rock' and player2 == 'scissors':
print('Player 1 wins')
elif player1 == 'scissors' and player2 == 'rock':
print('Player 2 wins')
elif player1 == 'paper' and player2 == 'scissors':
print('Player 2 wins')
elif player1 == 'scissors' and player2 == 'paper':
print('Player 1 wins')
elif player1 == 'scissors' and player2 == 'scissors':
print('Tie')
winner()
当我运行此代码时,它会询问“玩家 1?”不接受石头、纸或剪刀以外的任何东西。然后它继续为 player2 做同样的事情。然而,这是代码结束的地方,它不会运行我的 elif 语句并打印哪个玩家获胜。
编辑:已解决。感谢您帮助初学者。我完全忘记了返回字符串并将它们分配给变量。
【问题讨论】:
-
等一下,
player1或player2不是变量,它们是函数引用……而您是在将它们与字符串进行比较?如果您想在winner中使用它,我还想将x返回给被调用者。
标签: python