作业2
1.完成完整的温度转换程序
代码如下:
while True: a = int(input('如果华氏度转摄氏度按 1 \n如果摄氏度转华氏度按 2\n')) if a == 1: f=float(input('请输入华氏度:')) c=(f-32)*5/9 print(c) elif a == 2: c = float(input('请输入摄氏度:')) f = c*9/5+32 print(f) else: break
结果如下:
如果华氏度转摄氏度按 1 如果摄氏度转华氏度按 2 1 请输入华氏度:50 10.0 如果华氏度转摄氏度按 1 如果摄氏度转华氏度按 2 ... 注释:未跳出循环 请继续输入.
2.猜数字游戏(猜价格,猜年龄等)
代码如下:
a=15 print('猜字游戏') while True: b=int(input('请输入数字:')) if b > a: print('你输入的数字太大') elif b < a: print('你输入的数字太小') else: print(('你输入正确')) break
结果如下:
猜字游戏 请输入数字:16 你输入的数字太大 请输入数字: ... 注释:未跳出循环,请一直输入数字直到break.
3.解析身份证号、学号不同片段的含义
代码如下:
a=input('请输入你的身份证号码或者学号:') if len(a) == 18: b = int(input('你想知道省份请按 1 \n你想知道城市请按 2 \n你想知道区县请按 3\ \n你想知道生日请按 4 \n你想知道性别请按 5 ')) if b == 1: print(a[0:2]) elif b == 2: print(a[2:4]) elif b == 3: print(a[4:6]) elif b == 4: print(a[6:14]) elif b == 5: c=int(a[-2])%2 if c == 1: print('男性') else: print('女性') elif len(a) == 12: d = int(input('你想知道年级请按 1 \n你想知道专业请按 2 \n你想知道班级请按 3\ \n你想知道序号请按 4 ')) if d == 1: print(a[0:4]) if d == 2: print(a[4:6]) if d == 3: print(a[6:8]) if d == 4: print(a[8:12]) else: print('你输入的号码有误')
结果如下:
请输入你的身份证号码或者学号:201606050010 你想知道年级请按 1 你想知道专业请按 2 你想知道班级请按 3 你想知道序号请按 4 4 0010
4.字符串的:连接,重复,in判断
代码如下:
a='python' b='3' print(a+b) print(a*3) print('python' in a+b)
结果如下:
python3 pythonpythonpython True
5.用for循环产生一系列网址
代码如下:
for i in range(2,12): print( 'http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+'.html')
结果如下:
http://news.gzcc.cn/html/xiaoyuanxinwen/2.html http://news.gzcc.cn/html/xiaoyuanxinwen/3.html http://news.gzcc.cn/html/xiaoyuanxinwen/4.html http://news.gzcc.cn/html/xiaoyuanxinwen/5.html http://news.gzcc.cn/html/xiaoyuanxinwen/6.html http://news.gzcc.cn/html/xiaoyuanxinwen/7.html http://news.gzcc.cn/html/xiaoyuanxinwen/8.html http://news.gzcc.cn/html/xiaoyuanxinwen/9.html http://news.gzcc.cn/html/xiaoyuanxinwen/10.html http://news.gzcc.cn/html/xiaoyuanxinwen/11.html