题目
1. 使用Python语法在后台输出一个九九乘法表。 2. 写一个交互程序,实现摄氏和华氏温度的相互转换。摄氏温度转华氏温度的公式为: celsius * 1.8 = fahrenheit – 32。 提示:input() 函数接受用户的输入,然后通过输入的最后一个字符是 C,还是 F 来区分用户输入的是摄氏温度,还是华氏温度。 |
解答
- 循环
for i in range(1,10):
for j in range(1,i+1):
print(str(i)+'*'+str(j)+'='+str(i*j),end=' ')
print()
2. 函数
def c2f(celcius):
return celcius*1.8+32
def f2c(fahrenheit):
return (fahrenheit-32)/1.8
temperature=input('please input the temperature to be converted')
while(temperature!='q'):
try:
num=eval(temperature[:-1])
last=temperature[-1]
if last in ['c','C']:
print('result: ')
print(c2f(num))
elif last in ['f','F']:
print('result: ')
print(f2c(num))
except:
print('wrong input')
temperature=input('please input the temperature to be converted')
print('Bye!')