————CH05Homework————
0 .在 Python 中,int 表示整型,那你还记得 bool、float 和 str 分别表示什么吗?
answer:布尔型 浮点型 字符串
1. 你知道为什么布尔类型(bool)的 True 和 False 分别用 1 和 0 来代替吗?
answer:计算机只认识二进制,即0和1
2 .使用 int() 将小数转换为整数,结果是向上取整还是向下取整呢?
answer:向下取整
3 .我们人类思维是习惯于“四舍五入”法,你有什么办法使得 int() 按照“四舍五入”的方式取整吗?
answer:int(a + 0.5)
>>> a = 6.1
>>> int(a + 0.5)
6
>>> a = 6.5
>>> int(a + 0.5)
7
4 .取得一个变量的类型,视频中介绍可以使用 type() 和 isinstance(),你更倾向于使用哪个?
answer:python3更倾向于isinstance()
5. Python3 可以给变量命名中文名,知道为什么吗?
answer:因为中文编码方式是UTF-8,而Python3 支持UTF-8编码
6 .【该题针对零基础的鱼油】你觉得这个系列教学有难度吗?
answer:NO
Practice
0.针对视频中小甲鱼提到的小漏洞,再次改进我们的小游戏:
当用户输入错误类型的时候,及时提醒用户重新输入,防止程序崩溃。
s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False。
s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False。
s.isdigit() 所有字符都是数字,为真返回 Ture,否则返回 False。
s.islower() 所有字符都是小写,为真返回 Ture,否则返回 False。
s.isupper() 所有字符都是大写,为真返回 Ture,否则返回 False。
s.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。
s.isspace() 所有字符都是空白字符,为真返回 Ture,否则返回 False。
Code:
import random secret = random.randint(1,10) guess = 0 count = 3 print("----------START------------") while guess != secret and count > 0: print("All 3 chances,and you still have",count,"chances") temp = input("Please enter a number(1~10):") while temp.isdigit() != 1: print('input error!') temp = input('Please input again;') guess = int(temp) count = count - 1 if guess == secret: print("wonderfully congraduation!") else: if guess > secret: print("you guess bigger!") else: print("you guess smaller!") if count > 0: print("you have chance,you can continue!") else: print("No chance,byebye!") print("Game over!") 1 写一个程序,判断给定年份是否为闰年。(注意:请使用已学过的 BIF 进行灵活运用) 这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年。
Code:
print("----------闰年计算器-----------") print('输入的格式为:xxxx,如:2014') temp = input('请输入一个年份:') while temp.isdigit() != 1: print('输入错误,请重新输入!') temp = input("请再次输入:") year = int(temp) if (year/4 == int(year/4)) and (year/100 != int(year/100)): print(year,'是闰年') else: if year/400 == int(year/400): print(year,'是闰年') else: print(year, '不是闰年') print('结束!')