python做一个小游戏
时间: 2023-07-18 13:45:03 浏览: 102
好的,我可以帮你设计一个猜拳小游戏。
首先,我们需要让用户选择出拳的方式,可以使用`input()`函数获取用户输入。同时,我们可以使用`random`模块随机生成计算机出拳的方式。具体实现如下:
```python
import random
while True:
player = input("请出拳(石头/剪刀/布):")
computer = random.choice(["石头", "剪刀", "布"])
print("你出了:", player)
print("计算机出了:", computer)
if player == computer:
print("平局!")
elif player == "石头" and computer == "剪刀" or \
player == "剪刀" and computer == "布" or \
player == "布" and computer == "石头":
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
play_again = input("再玩一次吗?(Y/N):")
if play_again.upper() != "Y":
break
```
最后,我们可以将整个游戏放到一个函数中,并在主程序中调用它:
```python
import random
def rock_paper_scissors():
while True:
player = input("请出拳(石头/剪刀/布):")
computer = random.choice(["石头", "剪刀", "布"])
print("你出了:", player)
print("计算机出了:", computer)
if player == computer:
print("平局!")
elif player == "石头" and computer == "剪刀" or \
player == "剪刀" and computer == "布" or \
player == "布" and computer == "石头":
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
play_again = input("再玩一次吗?(Y/N):")
if play_again.upper() != "Y":
break
if __name__ == '__main__':
rock_paper_scissors()
```
这样,一个简单的猜拳小游戏就完成了。希望能对你有所帮助!
阅读全文