鱼C论坛

 找回密码
 立即注册
查看: 2778|回复: 3

[技术交流] python notes4-5

[复制链接]
发表于 2017-6-8 06:04:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-6-8 06:07:57 | 显示全部楼层
python notes4-5
http://bbs.fishc.com/thread-88327-1-1.html
(出处: 鱼C论坛)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-8 07:32:04 | 显示全部楼层


内容中包含代码:使用如下方法
链接中包含代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-8 08:01:27 | 显示全部楼层
1111
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 23:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表