文档介绍
利用 python 写 "猜数字","猜词语","谁是卧底" 这三个游戏, 从而快速掌握 python 编程的入门知识, 包括 python 语法 / 列表 / 元组 / 字典 / 流程控制 / 库函数等等.
环境参数
Linux 平台, python 3.4. 需要在 Linux 中把 python 3.4 编译一下, 这样编写 python 程序时保存为. py 格式的文件并添加执行权限再终端运行即可(原理跟 shell 脚本相同), 非常方便.
ps: 贴出来的 python 程序代码在 Windows 中也兼容, 只不过运行方式不是命令行, 而是要通过 python 软件. 在学习的时候, 可以直接在 python 软件上面输入 python 语句即可打印结果, 也就是说 python 无需编译即可运行, 很多人也称之为 "脚本" 语言.
设计实现
猜数字
很多人在聚餐时都玩过猜数字游戏, 由某人随机出一个指定范围内的数, 然后其他人一个一个猜, 猜的过程中区间不断缩小, 直到猜中为止.
这里的猜数字游戏就是用程序来代替那个出数字的人, 程序算法设计为:
1. 输入数字区间 --->2. 系统产生区间内的随机数 --->3. 玩家输入自己猜的数字 --->4. 比较玩家猜的与答案的高低并提示 --->5. 未猜中则回到 3, 猜中则提示猜次数
代码很少, 如下所示:
- #!/usr/local/bin/python3
- import random
- bot=int(input('Set range bottom\n'))
- top=int(input('Set range top\n'))
- rand=random.randint(bot,top)
- print ('Random number in ['+str(bot)+','+str(top)+'] generated!')
- num=int(input('###Guess the number###\n'))
- cnt=1
- while (num!=rand):
- if (num<rand):
- print('*_* Lower than the answer')
- else:
- print('T_T Higher than the answer')
- num=int(input('###Guess the number###\n'))
- cnt=cnt+1
- print('^_^ You get the answer with [%d] times'%cnt)
第一行是要让 Linux 系统知道这是 python3 程序, random 是产生随机数的库函数, 要注意的是 python 对格式要求很严格, 因为它不用通过 {} 而是通过缩进量判断流程.
通过这个可以掌握基本的语法和编写规则了, 下面再试试有点难度的, 用到词组和循环控制等知识.
- #!/usr/local/bin/python3
- import time
- num = int(input('有多少组玩家 \ n'))
- guessWord = []
- correct = []
- # 定义列表, num+10 是不想让 guessWord 下标不合法
- for i in range(0,num+10):
- guessWord.append(0)
- correct.append(0)
- wordNum=10
- guessWord[0] = ['打情骂俏','海绵宝宝','娇媚','金鸡独立','狼吞虎咽','睡眼朦胧','鹤立鸡群','手舞足蹈','卓别林','穿越火线']
- guessWord[1] = ['眉飞色舞','英雄联盟','扭秧歌','偷看美女','大摇大摆','回眸一笑','市场营销','大眼瞪小眼','自恋','处女座']
- guessWord[2] = ['狗急跳墙','捧腹大笑','目不转睛','愁眉苦脸','左顾右盼','宫保鸡丁','升国旗','暗恋','臭袜子','趁火打劫']
- flag='n'
- for i in range(0,num):
- start = time.time()
- for k in range(0,wordNum):
- # 显示词语
- print (('%d.%s')%(k+1,guessWord[i][k]))
- flag = input('请答题, 答对请输入 y, 跳过请输入任意键')
- end = time.time()
- sec = end-start
- # 统计用时
- if (110<=sec<=120):
- print ('还有 10 秒钟')
- if (sec>=120):
- print ('时间到! 游戏结束')
- break
- if (flag=='y'):
- correct[i]=correct[i]+1
- continue
- else:
- continue
- str_temp=('第 %d 组答对数目:%d') % (i+1,correct[i])
- print (str_temp)
- #!/usr/local/bin/python3
- import random
- from spyword import spyword
- num=int(input('请输入玩家数(至少为 3)\n'))
- # 卧底玩家
- spy=random.randint(0,num-1)
- # 随机产生词语 定义词语列表 计算玩家票数的列表 统计死亡玩家的列表
- list_rand=spyword.popitem()
- Word=[]
- cnt=[]
- dead=[]
- # 给三个列表赋值
- for i in range(0,num):
- Word.append('a')
- cnt.append(0)
- dead.append(num+2)
- # 给玩家词语 其中 print 是调试用的, sanmeVote 是出现相同票数的标志, spyWin 是卧底胜利的判决条件
- for i in range(0,num):
- if (i==spy):
- Word[i]=str(list_rand[1])
- else:
- Word[i]=str(list_rand[0])
- print (Word[i])
- sameVote=0
- spyWin=0
- # 游戏开始
- for x in range(0,num-1):
- for k in range(0,num):
- if ((k not in dead) & (sameVote==0)):
- print ('%d 号玩家发言时间'%(k+1))
- print ('发言环节结束')
- # 将各位玩家的票数置 0
- for j in range(0,num):
- if (j not in dead):
- cnt[j]=0
- for j in range(0,num):
- if (j not in dead):
- vote2p=int(input('请 %d 号玩家投票'%(j+1)))-1
- cnt[vote2p]=cnt[vote2p]+1
- sameVote=0
- for y in range(0,num):
- if((cnt[y]==max(cnt)) & (y!=cnt.index(max(cnt)))):
- print ('不止一位玩家得到最高票数, 请这些玩家重新发言')
- sameVote=1
- if (sameVote==0):
- dead[x]=cnt.index(max(cnt))
- if (dead[x]==spy):
- print ('卧底得到最多票数, 游戏结束')
- spyWin=1
- break
- print ('%d 号玩家被冤死!'%(dead[x]+1))
- # 游戏结束
- if(spyWin==0):
- print ('只剩两名玩家, 卧底胜利!')
- >>> import os
- >>> os.getcwd()
来源: http://www.jianshu.com/p/0519365d27c8