0913作业
1.完成完整的温度转换程序
输入代码
while True: a = int( input("摄氏转华氏请按 1\n华氏转摄氏请按 2:\n")) if a == 1: celsius = float (input('请输入摄氏温度:')) fahrenheit = (celsius * 1.8) + 32 print ('{:.2f}摄氏温度转华氏温度为{:.2f}'.format(celsius, fahrenheit)) elif a == 2 : fahrenheit = float (input('请输入华氏温度:')) celsius = 5 / 9 *(fahrenheit - 32) print('{:.2f}华氏温度转化为摄氏温度为:{:.2f}\n'.format(fahrenheit, celsius)) else: break
运行结果:
2.猜数字游戏(猜价格,猜年龄等)
输入代码:
number = 8 b = 0 print('数字猜谜游戏!') while b != number: b = int(input('请输入你猜的数字\n')) if b == number: print('恭喜你!猜对了!') elif b < number: print('数字小了') else: print('数字大了')
运行结果:
3.解析身份证号、学号不同片段的含义
输入代码:
a = input('请输入你的身份证号码:\n' ) print('省份:{},市:{},区\县:{},出生年月日:{},识别码:{},'.format(a[:2],a[2:4],a[4:6],a[6:14],a[-1])) if int(a[-2])%2 == 0: sex = 'girl' else: sex = 'boy' age = 2018-int(a[6:10]) print("这位是"+"性别:"+sex+",年龄:"+str(age)+"的朋友。")
运行结果:
输入代码:
def main(): a="" while (len(a) != 12): a = input('请输入12位学号') print('年级{}专业{}班级{}序号{}'.format(a[:4],a[4:6],a[6:8],a[8:])) if __name__ == '__main__': main()
运行结果:
4.字符串的:连接,重复,in判断
输入代码:
def main(): print('123'+'456') print('123'*3) print('5' in '123456789') for i in range(1,5): print('http: // news.gzcc.cn / html / xiaoyuanxinwen / '+str(i)+'236.html') if __name__ == '__main__': main()
运行结果:
5.用for循环产生一系列网址
输入代码:
for i in range(242): print('http://news.gzcc.cn/html/xiaoyuanxinwen/'+str(i)+"236.html")
运行结果:
6.用for循环遍历字符串
输入代码:
a = 'famliy' for c in a: print(c)
运行结果: