python用if语句编写石头剪刀布程序
时间: 2023-07-24 20:32:48 浏览: 29
好的,以下是使用 `if` 语句编写的 Python 石头剪刀布游戏程序:
```python
import random
print("欢迎来到石头剪刀布游戏!")
while True:
player = input("请出拳(石头/剪刀/布):")
player = player.lower()
while player not in ["石头", "剪刀", "布"]:
player = input("输入有误,请重新出拳(石头/剪刀/布):")
player = player.lower()
computer = random.choice(["石头", "剪刀", "布"])
print("你出了 %s,电脑出了 %s。" % (player, computer))
if player == computer:
print("平局!")
elif player == "石头":
if computer == "剪刀":
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
elif player == "剪刀":
if computer == "布":
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
else:
if computer == "石头":
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
play_again = input("再玩一局?(是/否):")
play_again = play_again.lower()
while play_again not in ["是", "否"]:
play_again = input("输入有误,请重新输入(是/否):")
play_again = play_again.lower()
if play_again == "否":
break
print("游戏结束,谢谢参与!")
```
程序逻辑和前面的版本基本一致,只是在判断胜负时使用了嵌套的 `if` 语句。
相关推荐













