python编写程序,模仿猜拳游戏,要求输入两个用户的不同手型,判断输赢后输出。
时间: 2024-09-18 14:09:49 浏览: 33
在Python中,你可以创建一个简单的猜拳游戏(Rock-Paper-Scissors),通常玩家可以选择石头(Rock)、纸(Paper)或剪刀(Scissors)。下面是编写这个游戏的一个基本步骤:
```python
import random
def user_choice():
choices = ["rock", "paper", "scissors"]
while True:
user_input = input("请输入你的选择 (rock/paper/scissors),或者输入 'quit' 退出游戏:")
if user_input.lower() in choices or user_input.lower() == "quit":
return user_input.lower()
else:
print("无效的选择,请重新输入.")
def computer_choice():
return random.choice(["rock", "paper", "scissors"])
def game_round(user, computer):
if user == computer:
return "平局!"
elif (user == "rock" and computer == "scissors") or \
(user == "paper" and computer == "rock") or \
(user == "scissors" and computer == "paper"):
return f"{user} 赢了!"
else:
return f"{computer} 赢了!"
while True:
user_selection = user_choice()
if user_selection == "quit":
break
computer_selection = computer_choice()
result = game_round(user_selection, computer_selection)
print(result)
print("游戏结束!")
阅读全文