|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
lesson4
条件分支
程序改进,猜错的时候程序提示用户当前的输入比答案大了或者小了
>;>=;<;<=;==;!=
Python的条件分支语法
用缩进表达命令的条件所属性
If 条件 :
条件为真时执行操作
Else:
条件为假时执行的操作
Bsp:
代码磁铁
temp=input("type a number I'm thinking")
guess=int(temp)
if guess==8:
print("wacao,are you a worm in my stomach?")
print("But there's no gifts for you")
else:
if guess>8:
print("Buddy,it's too big")
else:
print("It's too small")
print('game over')
while cycle
The program should provide many chances for the users to give it a try
Find the place where while cycle program should take place in the following sentences:
改进1 限制游戏循环次数超过3次游戏结束
提示:
使用and逻辑操作符
----Python的and逻辑操作符可以将任意表达式连接在一起,并得到一个布尔类型的值(true or false)
比较运算符比逻辑运算符的优先级要大
改进2 引入外援
改进要求每次运行程序产生的答案 是随机运行的
我们需要引入外援:random 模块
使用前要先import random
这个random模块里面会有一个函数叫做randint(),ta 会返回一个随机的整数。
lesson5
Python的数据类型
整型
Python3中整型和长整型为一类,长度无限制
布尔类型
True=1;false=0
可以进行整型计算(实际应用不用这种做法)
Bsp:
>>> True * False
>>> 0
浮点型
与整型区别是有无小数点
e记法
1.5e10
1.5e-10
浮点数转为整数 python将小数点后面的数字截断,不按照四舍五入计算
获得关于类型的信息
type()
Bsp:
>>> a=520
>>> type(a)
<class 'int'>
>>> type(5.2)
<class 'float'>
>>> type(True)
<class 'bool'>
isinstance()确定数据类型
返回True or False
isinstance(obj, class_or_tuple, /)
Return whether an object is an instance of a class or of a subclass thereof.
A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
or ...`` etc.
>>> a= 'xiaojiayu'
>>> isinstance(a,str)
True |
评分
-
查看全部评分
|