按照要求改编了温度转换代码 为什么循环无法正常运行?
请用异常处理改造温度转换代码,使其能够接收并处理用户的任何输入。异常处理无法执行循环?
使用try...except...else语句,当没有捕获到异常时执行else语句,同时你需要在try语句中用断言来判断一下,符合条件则执行else中语句,否则执行except抛出异常。
try:
temp=input()
assert temp[-1] in ['F', 'f', 'C', 'c']
except:
print('invalid input')
else:
if temp[-1] in ['F','f']:
C=(eval(temp[0:-1])-32)/1.8
print('{:.2f}C'.format(C))
if temp[-1] in ['C', 'c']:
F=1.8*eval(temp[0:-1])+32
print('{:.2f}F'.format(F))
如果解答对你有帮助,请点击我回答的右上方采纳按钮采纳一下。
if elif 不走 except的 你要写else
你要把tempstr=input(..) 这句写在try...except外面
改了但是好像没有效果
[V$}DG.png)