TIM截图20180727113817.png
前提条件:电脑上已经按照好了Python3,没有请返回查看本文集中安装教程
猜数字游戏
第一回合,游戏内容
编写一段python程序,接收用户输入一个数字,若数字等于8,则打印“恭喜你,你才对了”,否则打印“很遗憾,你猜错了”
语法:
print函数
input函数
数据类型,int,字符串str
赋值运算符 ‘=’
比较运算符 ‘==’
if...else判断
'''guess Number games '''print('.....Game Start.....') temp =input('Please input a number \n') guess =int(temp)if guess==8: print('Good,you are right!')else: print('Sorry,you are wrong!') print('....Game over!....')
第二回合,改进游戏内容:
当数字不等于8时,若数字大于8,则打印“猜大了猜打了”,否则打印“猜小了猜小了”
语法:
if...elif...else判断
比较运算符 ‘>’
'''guess Number games '''print('.....Game Start.....') print('.....First,product a number..... ') temp =input('Please input a number \n') guess =int(temp)if guess==8: print('Good,you are right!')elif guess >8: print('Oh, too big! ')else: print('Oh, too small! ') print('Sorry,you are wrong!') print('....Game over!....')
第三回合,改进游戏内容:
用户猜错了可以继续再猜,让用户猜三次,三次猜不出后游戏结束
语法:
while循环
'''guess Number games '''print('.....Game Start.....') count =0while count<3: temp =input('Please input a number \n') guess =int(temp) count=count+1 if guess==8: print('Good,you are right!') elif guess>8: print('Oh, too big!') else: print('Oh, too small! ') print('Sorry,you are wrong!') print('....Game over!....')
第四回合,改进游戏内容:
用户猜了一次就知道正确数字是8,再次猜就没什么意思了,并且上一回合里,若用户在第一,第二次就猜中了,游戏也没结束。所以改进游戏让每次随机产生一个1~10的数字,让用户猜三次,猜中就结束游戏。
语法:
print函数
if...elif...else判断
while循环
break
random包
random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
'''guess Number games '''import random print('.....Game Start.....') count =0secret =random.randint(1,10)while count<3: temp =input('Please input a number \n') guess =int(temp) count=count+1 if guess==secret: print('Good,you are right!') break elif guess>secret: print('Oh, too big!') else: print('Oh, too small! ') print('Sorry,you are wrong!') print('....Game over!....')
第五回合,改进游戏内容:
用户猜错了,可以继续再猜,最多猜三次,猜对了则游戏进入下一轮猜数字,直到用户不想玩了,输入‘q’退出,游戏才结束
语法:
print函数
if...elif...else判断
while循环
break
type内置函数
逻辑运算符
random包
random.randint(a, b),用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
'''guess Number games '''import random print('.....Game Start.....') flag =Truewhile flag: secret =random.randint(1,10) count=0 print('New game again~') while count<3: temp =input('Please input a number,quit with \'q\' \n') if(type(temp)==str and temp=='q'): flag =False break guess =int(temp) count=count+1 print('count=',count) if guess==secret: print('Good,you are right!') count=0 break elif guess>secret: print('Oh, too big!') else: print('Oh, too small! ') print('Sorry,you are wrong!') print('....Game over!....')
作者:君临天下夜未央
链接:https://www.jianshu.com/p/96f755a54a6f
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦