生成Python人机猜拳游戏代码
时间: 2023-05-26 10:01:23 浏览: 118
猜拳游戏_人机猜拳_互动游戏_
A:
```
import random
options = ["Rock", "Paper", "Scissors"]
print("Welcome to the Rock-Paper-Scissors game!")
while True:
player_choice = input("What's your choice? (Rock/Paper/Scissors): ")
while player_choice not in options:
player_choice = input("Invalid choice! What's your choice? (Rock/Paper/Scissors): ")
computer_choice = random.choice(options)
print("The computer chose: " + computer_choice)
if player_choice == computer_choice:
print("It's a tie!")
elif player_choice == "Rock":
if computer_choice == "Paper":
print("You lose!")
elif computer_choice == "Scissors":
print("You win!")
elif player_choice == "Paper":
if computer_choice == "Rock":
print("You win!")
elif computer_choice == "Scissors":
print("You lose!")
elif player_choice == "Scissors":
if computer_choice == "Rock":
print("You lose!")
elif computer_choice == "Paper":
print("You win!")
play_again = input("Do you want to play again? (Y/N): ")
if play_again.lower() != "y":
break
print("Thanks for playing Rock-Paper-Scissors!")
```
这是一个简单的人机猜拳游戏代码,玩家可以选择“石头”、“剪刀”或“布”,计算机会随机选择一个选项。游戏结果会显示在屏幕上,然后询问玩家是否要继续玩下去。是的话游戏继续,否则退出游戏。
阅读全文