Python实现猜拳游戏(完整源码)
Python是一门十分流行的编程语言,对于初学者来说,写一个小游戏是很不错的练手方式。在这里我将为大家演示如何在Python中实现简单的猜拳游戏。代码如下:
import random
name = input("请输入您的名字:")
print("欢迎来到猜拳游戏,%s!" % name)
while True:
choices = ["石头", "剪刀", "布"]
computer_choice = random.choice(choices)
user_choice = input("请选择您的出拳(石头/剪刀/布):")
print("电脑出了:%s" % computer_choice)
if computer_choice == user_choice:
print("平局!")
elif user_choice == "石头" and computer_choice == "剪刀":
print("恭喜您获胜!")
elif user_choice == "剪刀" and computer_choice == "布":
print("恭喜您获胜!")
elif user_choice == "布" and computer_choice == "石头":
print("恭喜您获胜!")
else:
print("很遗憾,您输了。")
play_again = input("是否再来一局?(y/n)")
if play_again.lower() != "y":
brea