py2、编写猜拳游戏,要求输入两个用户不同手型,判断输赢后输出。
时间: 2024-12-19 11:17:57 浏览: 12
在Python 2中编写猜拳游戏(Rock-Paper-Scissors),首先需要定义玩家输入的手型(rock, paper, scissors)以及判断输赢的规则。以下是简化的代码示例:
```python
def get_player_choice():
choices = ['rock', 'paper', 'scissors']
return raw_input('请输入你的选择 (rock/paper/scissors): ').lower().strip() in choices
def determine_winner(player1, player2):
if player1 == player2:
return "平局"
elif (player1 == 'rock' and player2 == 'scissors') or \
(player1 == 'paper' and player2 == 'rock') or \
(player1 == 'scissors' and player2 == 'paper'):
return "你赢了!"
else:
return "对方赢了!"
def main():
player1 = get_player_choice()
player2 = get_player_choice()
print(determine_winner(player1, player2))
if __name__ == '__main__':
main()
```
在这个程序中,`get_player_choice`函数用于获取用户的输入并验证是否有效。`determine_winner`函数则根据猜拳规则判断输赢。最后,在`main`函数中,玩家分别出拳,然后根据结果输出赢家。
阅读全文