学习要点:
1.第一个改进要求:猜错的时候程序提示用户当前的输入比答案大了还是小了。
2.第二个改进要求:程序应该提供多次机会给用户猜测,专业点来说就是程序需要重复运行某些代码。
3.第三个改进要求:每次运行程序产生的答案是随机的:(引入模块random模块)
这个random模块里边有个函数叫做:randint(),ta会返回一个随机的整数。
if 条件:
条件为真(true)执行的操作。 else: 条件为假(False)执行的操作。
python的while循环语法:
while 条件 :
条件为真(True)执行的操作。
学习代码如下:
import random secret = random.randint(1,20) print('---------------------和你玩个小游戏---------------------------') temp = input("请猜猜我心中的数字:") guess = int(temp) while guess != secret: temp = input("hi呀,你猜错了,相信你可以的继续猜:") guess = int(temp) if guess == secret: print("嗨呀,难道你是我肚子一条蛔虫吗") print("哈哈,猜对也没有奖励。") else: if guess > secret: print("不好意思,您猜的数字大了哈") else: if 1 <= guess < 21: print('你猜的数字很接近,继续加油。') else: print("不好意思,您猜的数字小了哈") print("游戏结束,不玩啦")
005-Python的数据类型。
init() float() str() e记法
例如:
a = 0.000000000008 a 8e-12 a = 9e10 a 90000000000.0
type() isinstance()
例题:
a = '520' type(a) <class 'str'> a = 520 type (a) <class 'int'> isinstance(a,int) True isinstance(a,float) False
测试题:
1/25,幂级运算优先级高于其他算术操作符合逻辑操作符,但是幂级运算有一点比较特殊
幂运算操作符比其左侧的一元操作符优先级高,比其右侧的一元操作符优先级低。
例:-52=-25,是-(52),而不是(-5)**2
-52 -25 5-2 0.04
1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9) == 0 or 0 or 4 or 6 or 9 == 4
例如:
3 or 5 3 3 and 5 5 0 and 5 0 0 or 5 5
曲求全”代替“%”的功能呢?
print("-----------------------还记得我们上节课那个求闰年的作业吗?如果还没有学到“求余”操
作,还记得用什么方法可以“委曲求全”代替“%”的功能呢?---------------------------------") years=input("请输入一个年数判断是否闰年:") year=int(years) a=year/4 b=year-4*a if (b==0): print("闰年") else: print("不是闰年")
i=1 while i <= 100: if (i % 2) != 0: print(i) i+=1 else: print('end')