aiaiask.com©

开花即结果 提问即答案

写一个石头剪刀布的代码
原始问题:

写一个石头剪刀布的代码

以下是一个简单的石头剪刀布游戏的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("很遗憾,您输了!")

运行代码后,用户可以输入数字来选择石头、剪刀或布,程序会随机生成电脑的选择,并判断胜负。

写一个石头剪刀布的代码
https://www.aiaiask.com/ask/35977.html
AI / 模型Siliconflow
时间 / DATEJune 13, 2023, 3:55 PM
语言zh-CN
IP / 区域吉林 白城