写一个猜拳的代码
- User
写一个猜拳的代码
Assistant
以下是一个简单的猜拳游戏代码,使用Python编写。这个代码包括玩家输入、电脑随机选择、判断胜负以及游戏循环。
import random def get_computer_choice(): """获取电脑的随机选择""" choices = ['rock', 'paper', 'scissors'] return random.choice(choices) def get_winner(player_choice, computer_choice): """判断胜负""" if player_choice == computer_choice: return 'Tie' elif (player_choice == 'rock' and computer_choice == 'scissors') or \ (player_choice == 'paper' and computer_choice == 'rock') or \ (player_choice == 'scissors' and computer_choice == 'paper'): return 'Player wins' else: return 'Computer wins' def get_player_choice(): """获取玩家的输入并验证""" choices = ['rock', 'paper', 'scissors'] player_choice = input("Enter your choice (rock, paper, scissors): ").lower() while player_choice not in choices: print("Invalid choice, please try again.") player_choice = input("Enter your choice (rock, paper, scissors): ").lower() return player_choice def play_game(): """游戏循环""" while True: player_choice = get_player_choice() computer_choice = get_computer_choice() print(f"Computer chose: {computer_choice}") result = get_winner(player_choice, computer_choice) print(result) play_again = input("Do you want to play again? (yes/no): ").lower() if play_again != 'yes': break if __name__ == "__main__": play_game()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.