阅读量:0
当然可以!以下是一个简单的Python小游戏示例:猜拳游戏。在这个游戏中,玩家将与计算机进行猜拳(石头、剪刀、布)。
import random def get_computer_choice(): choices = ['石头', '剪刀', '布'] return random.choice(choices) def get_player_choice(): choice = input("请输入你的选择(石头、剪刀、布):") while choice not in ['石头', '剪刀', '布']: choice = input("输入无效,请重新输入你的选择(石头、剪刀、布):") return choice def determine_winner(player_choice, computer_choice): if player_choice == computer_choice: return "平局!" elif (player_choice == "石头" and computer_choice == "剪刀") or \ (player_choice == "剪刀" and computer_choice == "布") or \ (player_choice == "布" and computer_choice == "石头"): return "你赢了!" else: return "你输了!" def play_game(): player_choice = get_player_choice() computer_choice = get_computer_choice() print(f"\n你的选择是:{player_choice}") print(f"计算机的选择是:{computer_choice}") result = determine_winner(player_choice, computer_choice) print(result) if __name__ == "__main__": play_game()
在这个游戏中,玩家输入他们的选择(石头、剪刀或布),计算机随机生成一个选择,然后程序会判断并宣布赢家。你可以运行这段代码,并根据提示来玩猜拳游戏。