Python 05
2017-12-07 本文已影响0人
Jack_Hsin
一. 课上代码
>>> '520' + '1314'
'5201314'
>>> 520 + 1314
1834
>>> a = 0.0000000000000000000000005
>>> a
5e-25
>>> 150000000000
150000000000
>>> 1.5e11
150000000000.0
>>> 15e10
150000000000.0
>>> 1.5e4
15000.0
>>> True + True
2
>>> True + False
1
>>> True * True
1
>>> True / False
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
True / False
ZeroDivisionError: division by zero
>>> a = '520'
>>> b = int(a)
>>> b
520
>>> b = int('Jack')
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
b = int('Jack')
ValueError: invalid literal for int() with base 10: 'Jack'
>>> a = 5.99
>>> c = int(a)
>>> c
5
>>> a = '520'
>>> b = float(a)
>>> b
520.0
>>> a = 520
>>> b = float(a)
>>> b
520.0
>>> a = 5.99
>>> b = str(a)
>>> b
'5.99'
>>> c = str(5e19)
>>> c
'5e+19'
>>> a = '520'
>>> type(a)
<class 'str'>
>>> type(5.2)
<class 'float'>
>>> type(True)
<class 'bool'>
>>> a = 'Jack'
>>> isinstance(a, str)
True
#用isinstance()函数获得关于类型的信息
>>> isinstance(a, int)
False
>>> isinstance(320.24, float)
True
>>> isinstance(320.4, bool)
False
二. 测试题
- 使用int()将小数转换为整数,结果是向上取整还是向下取整?
对于正数,小数取整会采用比较暴力的截断方式,即向下取整
对于负数则正好相反,是向上取整
- 有什么办法使得int()按照“四舍五入”的方式取整吗?
#个人答案
num = float(input("Input a number:"))
num1 = int(num)
if num - num1 >= 0.5:
print(num1 + 1)
else:
print(num1)
#参考答案
5.4: int(5.4 + 0.5) == 5
5.6: int(5.6 + 0.5) == 6
三. 动动手
- 改进上一课的游戏,当用户输入错误类型的时候,即使提醒用户重新输入,防止程序崩溃
#tips
#s为字符串
s.isalnum() 所有字符都是数字或者字母,为真返回True,否则返回False
s.isalpha() 所有字符都是字母,为真返回True,否则返回False
s.isdigit() 所有字符都是数字,为真返回True,否则返回False
s.islower() 所有字符都是小写,为真返回True,否则返回False
s.isupper() 所有字符都是大写,为真返回True,否则返回False
s.istitle() 所有单词都是首字母大写,为真返回True,否则返回False
s.isspace() 所有字符都是空白字符,为真返回True,否则返回False
>>> s = 'I love you'
>>> s.islower()
False
>>> s = 'I LOVE YOU'
>>> s.upper()
'I LOVE YOU'
#个人代码
temp = input("Input a number:")
while temp.isdigit() is False:
print("Sorry, your input is illegal, ", end = "")
temp = input("Try again:")
#参考代码
import random
times = 3
secret = random.randint(1, 10)
print("Guess a number:", end = " ")
guess = 0
while guess != secret and times > 0:
temp = input()
if temp.isdigit() is True:
guess = int(temp)
if guess == secret:
print("Awesome!")
else:
if guess > secret:
print("Bigger!")
else:
print("Smaller!")
if times > 1:
print("Try again:", end = " ")
else:
print("Opportunities out!")
else:
print("Sorry, please input an integer:", end = " ")
times = times - 1
print("Game over.")
- 写一个程序,判断给定年份是否为闰年
#个人代码
temp = input("Please input one year:")
while not temp.isdigit():
temp = input("Sorry, your input is not correct, try again:")
year = int(temp)
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(temp + ' is runnian')
else:
print(temp + " is not runnian")
#参考代码
temp = input("Please input one year:")
while not temp.isdigit():
temp = input("Your input is incorect, please input an integer:")
year = int(temp)
if year / 400 == int(year / 400):
print(temp + ' is runnian')
else:
if(year / 4 == int(year / 4)) and (year / 100 != int(year / 100)):
print(temp + ' is runnian')
else:
print(temp + ' is not runnian')
#有一处不太懂,这是为啥?
>>> 2000 / 4
500.0
>>> int(2000 / 4)
500
>>> 2000 / 4 == int(2000 / 4)
True