Python小游戏-石头剪刀布(和电脑斗智斗勇)

简介: 小时候经常玩一个游戏,几个人把手背在后面,出石头,剪刀,布,现在身边没小伙伴了,就只能跟电脑斗智斗勇了,利用random的随机数来模拟随机出拳random.randint(1,3) -- 返回1~3之间的整数

小时候经常玩一个游戏,几个人把手背在后面,出石头,剪刀,布,现在身边没小伙伴了,就只能跟电脑斗智斗勇了,利用random的随机数来模拟随机出拳

random.randint(1,3)    -- 返回1~3之间的整数

代码如下

# 猜拳游戏
import random
# 获取玩家出的拳
player = int(input('玩家请出拳 石头(1)/剪刀(2)/布(3)'))
# 输出玩家出的拳
if player == 1:
    print('玩家出的拳是:石头')
elif player == 2:
    print('玩家出的拳是:剪刀')
elif player == 3:
    print('玩家出的拳是:布')
else:
    print('你出的啥玩意?')
# 电脑出拳
computer = random.randint(1, 3)
# 输出电脑出的拳
if computer == 1:
    print('电脑出的拳是:石头')
elif computer == 2:
    print('电脑出的拳是:剪刀')
elif computer == 3:
    print('电脑出的拳是:布')
else:
    print('电脑坏了')
# 判断胜负
if ((player == 1 and computer == 2)
        or (player == 2 and computer == 3)
        or (player == 3 and computer == 1)):
    print('垃圾电脑,敢跟我斗?')
elif player == computer:
    print('垃圾电脑,敢跟我出一样的!')
else:
    print('垃圾电脑,不讲武德,看我不砸了你!')

image.png

相关文章
|
2月前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
2月前
|
Python
python小游戏4
python小游戏4
|
2月前
|
Python
python小游戏7
python小游戏7
|
2月前
|
Python
python小游戏6
python小游戏6
|
2月前
|
Python
python小游戏5
python小游戏5
|
2月前
|
Python
python小游戏1
python小游戏1
|
2月前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
52 0
|
2月前
|
Python
python小游戏3
python小游戏3
|
2月前
|
Python
python小游戏
python小游戏
|
19天前
|
开发者 Python
入门实战丨Python小游戏经典案例
入门实战丨Python小游戏经典案例
22 4