用python制作剪刀石头布的小游戏

简介: 用python制作剪刀石头布的小游戏

1问题

在python中我们学习了条件语句,那么我们是否可以通过python中条件判断的功能来写出可以判断胜负的剪刀石头布小游戏呢?


2方法

导入随机函数,保证胜负的随机性


设置对应数值,写好判断输赢的条件语句


运行并查看结果

代码清单 1

import random  
num = 1
yin_num = 0
shu_num = 0
while num <= 3:
   if shu_num == 2 or yin_num == 2:
       break
   user = int(input('请出拳 0(石头) 1(剪刀) 2(布)'))
   if user > 2:
       print('不能出大于2的值')
   else:
       data = ['石头', '剪刀', '布']
       com = random.randint(0, 2)
       print("您出的是{},电脑出的是{}".format(data[user], data[com]))
       if user == com:
           print('平局')
           continue
       elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):
           print('你赢了')
           yin_num += 1
       else:
           print('你输了')
           shu_num += 1
   num += 1


3结语

通过运行结果发现这样的思路是完全可行的,但是我们并没有学过文字直接与文字比较的方法,所以需要用数字来替代文字进行比较才可以得到正确的结果。

目录
相关文章
|
16小时前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
16小时前
|
Python
python小游戏7
python小游戏7
|
16小时前
|
Python
python小游戏6
python小游戏6
|
16小时前
|
Python
python小游戏5
python小游戏5
|
16小时前
|
Python
python小游戏4
python小游戏4
|
16小时前
|
Python
python小游戏3
python小游戏3
|
16小时前
|
Shell Python
python|闲谈2048小游戏和数组的旋转及翻转和转置
python|闲谈2048小游戏和数组的旋转及翻转和转置
28 1
|
11月前
|
Java Python
【python小游戏】用python写一款小游戏--贪吃蛇
【python小游戏】用python写一款小游戏--贪吃蛇
262 0
python小游戏,pygame手写贪吃蛇
python小游戏,pygame手写贪吃蛇
|
Python
Python小游戏(贪吃蛇)
Python小游戏(贪吃蛇)玩法:童年经典,普通魔术也没啥意思,小时候玩的也是加速的。
125 0
Python小游戏(贪吃蛇)