猜数字小游戏
2018-03-30 17:42
176 查看
参考脚本如下,给出一个区间,最大值(top)和最小值(bottom),
然后在这个区间里猜。
【具体脚本】
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"))
count=1
while(num!=rand):
if(num<rand):
print("lower than the answer")
else:
print("higher than the answer")
count=count+1
num=int(input("guess again"))
print("you get the answer with[%d]times"%count)【结果】
注意:
1-输出部分
print("random number in["+str(bot)+","+str(top)+"]generated!")这句话的意思是输出,完整的两个引号的内容,是要输出的
比如“random number in [”以及“,”(一个逗号),包括“]generated ”凡是绿色的字都代表要输出的具体内容
如何把赋予的值带入这个表达式,即在[bottom,top]的区间引用两个变量值时,除了两个变量拼写,
方式是前后都要加上+,是连字符的意思。代表完整输出一句话。
2-区间数值
rand=random.randint(bot,top)随机数的区间长度,用过random的randint方法来计算具体某个数值,再和下面输入的字符做比较。
while(num!=rand):3-语法上的缩进问题
在python里面缩进就是语法的一部分,要严格区分,包括条件判断语句中的缩进,代表了哪一个条件从句
严格注意缩进。
以上
然后在这个区间里猜。
【具体脚本】
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"))
count=1
while(num!=rand):
if(num<rand):
print("lower than the answer")
else:
print("higher than the answer")
count=count+1
num=int(input("guess again"))
print("you get the answer with[%d]times"%count)【结果】
注意:
1-输出部分
print("random number in["+str(bot)+","+str(top)+"]generated!")这句话的意思是输出,完整的两个引号的内容,是要输出的
比如“random number in [”以及“,”(一个逗号),包括“]generated ”凡是绿色的字都代表要输出的具体内容
如何把赋予的值带入这个表达式,即在[bottom,top]的区间引用两个变量值时,除了两个变量拼写,
方式是前后都要加上+,是连字符的意思。代表完整输出一句话。
2-区间数值
rand=random.randint(bot,top)随机数的区间长度,用过random的randint方法来计算具体某个数值,再和下面输入的字符做比较。
while(num!=rand):3-语法上的缩进问题
在python里面缩进就是语法的一部分,要严格区分,包括条件判断语句中的缩进,代表了哪一个条件从句
严格注意缩进。
以上
相关文章推荐
- python第二天 ( 猜数字小游戏)
- 猜数字小游戏
- 实现一个猜数字的小游戏,rand和srand的使用
- 首个python程序,一个猜数字的小游戏 ^0^
- 简单的小游戏(猜数字)
- 用js实现猜数字小游戏
- 猜数字小游戏(仅限小朋友哦)
- 猜数字小游戏
- Java编写猜数字小游戏
- 利用JavaScript写猜数字小游戏
- java猜数字小游戏:任意输入数字,判断与系统随机产生数字是否相等。
- C语言小游戏:猜数字
- 猜数字小游戏 C语言实现
- 猜数字小游戏
- 小游戏 猜数字 (简单版)
- 首个python程序,一个猜数字的小游戏 ^0^
- 用javascript写的猜数字小游戏,超简单
- java实践:小游戏:猜数字、判断回文数 。
- 猜数字的小游戏
- go语言实现猜数字小游戏的方法