天天看点

9.13作业

摄氏温度和华氏温度的相互转化

while True:
    a = int(input('摄氏转华氏请按1\n华氏转摄氏请按 2:\n'))

    if a ==1:
        #用户输入摄氏温度
        celsius = float(input('输入摄氏温度:'))

        #计算华氏温度
        fahrenheit = (celsius + 1.8) + 32  #f=c*9/5+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;      

运行结果

9.13作业

用字符串表示自己的学号,分隔出年级、专业、班级、序号

s ='201606050038'
a=s[:4]
b=s[4:6]
c=s[8:]
d=s[10:]
print('年级{}'.format(a))
print('学院{}'.format(b))
print('班级{}'.format(c))
print('序号{}'.format(d))      

运行结果为

9.13作业

身份证号提取

s ='440112200010101234'
a=s[:2]
b=s[2:4]
c=s[4:6]
age=2018-int(s[6:10])
print('{省份}',format(a))
print('{地区}',format(b))
print('{县区}',format(c))
if int(s[-2])%2 == 0:
    sex = '女'
else:
    sex='男'
    print(sex)      

运行结果为

9.13作业

字符串链接重复判断

s='疯疯铃'
print(s + "是魔鬼"*3)
print('是'in'是魔鬼')      

运行结果为

9.13作业

网址

for i in range(2,10):
    print ('http://news.gzcc.cn/html/xiaoyuanxinwen/'+ str(i)+'.html')      

运行结果为

9.13作业

猜数游戏

while True:
   a = 2
   guess= int(input("请输入你猜的数字:\n"))
   if guess > a :

        print("太大了\n")
   elif guess < a :
         print("太小了\n")
   else:
        print("猜对了\n")
        break;      

运行结果为

9.13作业

转载于:https://www.cnblogs.com/fanfanfan/p/9639690.html