石头剪刀布代码Python
时间: 2024-06-15 10:02:33 浏览: 135
石头剪刀布是一个经典的计算机游戏,使用Python编写非常直观。以下是一个简单的石头剪刀布游戏的代码示例:
```python
import random
def game():
options = ["rock", "paper", "scissors"]
player_choice = input("Choose (rock, paper, scissors): ").lower()
if player_choice not in options:
print("Invalid choice! Please choose rock, paper, or scissors.")
return
computer_choice = random.choice(options)
print(f"Computer chose: {computer_choice}")
if player_choice == computer_choice:
print("It's a 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"):
print("You win!")
else:
print("You lose!")
if __name__ == "__main__":
game()
```
在这个代码中,玩家输入他们的选择,然后计算机随机选择一个。程序会根据游戏规则(石头胜剪刀,剪刀胜布,布胜石头)判断输赢并给出结果。
阅读全文