原始问题:
写一个石头剪刀布的代码
以下是一个简单的石头剪刀布游戏的Python代码:
import random
print("欢迎来到石头剪刀布游戏!")
print("请输入您的选择:")
print("1. 石头")
print("2. 剪刀")
print("3. 布")
# 获取用户输入
user_choice = int(input("请输入数字:"))
# 判断用户输入是否合法
if user_choice not in [1, 2, 3]:
print("输入无效,请重新输入!")
else:
# 生成电脑随机选择
computer_choice = random.randint(1, 3)
# 输出电脑选择
if computer_choice == 1:
print("电脑选择了石头!")
elif computer_choice == 2:
print("电脑选择了剪刀!")
else:
print("电脑选择了布!")
# 判断胜负
if user_choice == computer_choice:
print("平局!")
elif user_choice == 1 and computer_choice == 2:
print("恭喜您,您赢了!")
elif user_choice == 2 and computer_choice == 3:
print("恭喜您,您赢了!")
elif user_choice == 3 and computer_choice == 1:
print("恭喜您,您赢了!")
else:
print("很遗憾,您输了!")运行代码后,用户可以输入数字来选择石头、剪刀或布,程序会随机生成电脑的选择,并判断胜负。
Prev:如何认识高质量发展对我国发展的战略意义