Python123第一周自由练习题

#编程小白,在线记录

实例1,温度转换

# TemStrCovert

TemStr = input()
if TemStr[-1] in ['C','c']:
    F = (eval(TemStr[0:-1])*1.8 + 32)#eval而不是elval
    print("{:.2f}F".format(F))#这里表示将输出结果变为两位小数
elif TemStr[-1] in ['F','f']:
    C = (eval(TemStr[0:-1])-32)/1.8 
    print("{:.2f}C".format(C))
else:
    print("输入格式错误")

实例2,数字形式转换

# NumberCovert
#CC:Chinese Character
Num = input()
CC = ["零","一","二","三","四","五","六","七","八","九"]
for i in Num:
    t = eval(i)#eval可将字符串类型转换为数值类型,相当于Str2Num
    print(CC[t],end="")#由于print表示打印一行,结尾加换行符;end=""表示在末尾加空字符串,表示语句未结束;

实例3,温度转换2

# TemStr2
TemStr = input()
if TemStr[0] in ['C','c']:
    F = (eval(TemStr[1:])*1.8 + 32)#eval而不是elval
    print("F{:.2f}".format(F))   
elif TemStr[0] in ['F','f']:
    C = (eval(TemStr[1:])-32)/1.8 #[1:]表示提取除首字符外的所有剩余字符串,“切片”
    print("C{:.2f}".format(C))
else:
    print("输入格式错误")

实例4,货币转换

# RMB2USD
Money = input()
if Money[0:3] in ["RMB"]:
    USD = eval(Money[3:])/6.78
    print("USD{:.2f}".format(USD))
elif Money[0:3] in ["USD"]:
    RMB = 6.78*eval(Money[3:])
    print("RMB{:.2f}".format(RMB))

你可能感兴趣的:(python)