import random

print("欢迎来玩石头剪刀布游戏!")
print("请输入您的选择:")
print("1. 石头 2. 剪刀 3. 布")

player_choice = int(input())

if player_choice not in [1, 2, 3]:
    print("请输入有效数字!")
else:
    computer_choice = random.randint(1, 3)
    print("电脑选择了:", computer_choice)

    if player_choice == computer_choice:
        print("平局!")
    elif player_choice == 1 and computer_choice == 2:
        print("您赢了!")
    elif player_choice == 2 and computer_choice == 3:
        print("您赢了!")
    elif player_choice == 3 and computer_choice == 1:
        print("您赢了!")
    else:
        print("电脑赢了!")

注意:上述代码是基于Python 3的,如果是Python 2的话需要做一些修改,比如input()函数应改为raw_input()函数。

写一个石头剪刀布的Python代码

原文地址: https://www.cveoy.top/t/topic/bwne 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录