【发布时间】:2017-06-08 20:50:18
【问题描述】:
我正在练习条件和逻辑运算符。
如何让下面的石头、纸、剪刀游戏打印“这不是一个有效的对象选择”。在玩家 1 的输入之后,如果玩家 1 输入了一个无效的对象?现在,直到两个玩家都输入一个对象,该字符串才被打印出来。
另外,有什么建议可以让下面的代码更优雅吗?
player1 = input('Player 1? ')
player2 = input('Player 2? ')
if (player1.lower() == 'rock' and
player2.lower() == 'rock'):
print('Tie.')
elif (player1.lower() == 'rock' and
player2.lower() == 'paper'):
print('Player 2 wins.')
elif (player1.lower() == 'rock' and
player2.lower() == 'scissors'):
print('Player 1 wins.')
elif (player1.lower() == 'paper' and
player2.lower() == 'paper'):
print('Tie.')
elif (player1.lower() == 'paper' and
player2.lower() == 'scissors'):
print('Player 2 wins.')
elif (player1.lower() == 'paper' and
player2.lower() == 'rock'):
print('Player 1 wins.')
elif (player1.lower() == 'scissors' and
player2.lower() == 'scissors'):
print('Tie.')
elif (player1.lower() == 'scissors' and
player2.lower() == 'rock'):
print('Player 2 wins.')
elif (player1.lower() == 'scissors' and
player2.lower() == 'paper'):
print('Player 1 wins.')
else:
print('This is not a valid object selection.')
【问题讨论】:
标签: python-3.x