欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python脚本:石头剪刀布

程序员文章站 2024-02-20 11:12:52
...

python脚本:石头剪刀布

代码如下(示例):

# coding=utf-8
import random

# 用户出拳,1=石头;2=剪刀;3=布
player = int(input("请出拳:1=石头;2=剪刀;3=布:"))

if player != 1 and player != 2 and player != 3:
    print('请好好出拳,别搞事')
    exit(0)

# 电脑随机出拳
ai = random.randint(1, 3)

if player == 1:
    player_c = '石头'
elif player == 2:
    player_c = '剪刀'
else:
    player_c = '布'

if ai == 1:
    ai_c = '石头'
elif ai == 2:
    ai_c = '剪刀'
else:
    ai_c = '布'

print('玩家出拳:%s;ai出拳:%s' % (player_c, ai_c))

# 判断胜负

if ((player == 1 and ai == 2)
        or (player == 2 and ai == 3)
        or (player == 3 and ai == 1)):
    print('电脑弱爆了')
elif ((player == 1 and ai == 3)
      or (player == 2 and ai == 1)
      or (player == 3 and ai == 2)):
    print('我不服,再来一次')
else:
    print('不要走,决战到天亮')

相关标签: python脚本 python