Python多条件匹配时出现“argument of type ‘int‘ is not iterable”

错误信息表现为
在这里插入图片描述

n=input("要转换的温度选项;1,摄氏度转华氏度;2,华氏度转摄氏度:")
if n in (1:
    S=input("摄氏度为:")
    H=1.8*float(S)+32
    print("华氏度为:",format(H))
elif n in (2):
    H=input("华氏度为:") 
    S=(float(H)-32)/1.8
    print("摄氏度为:",format(S))


解决办法
把“if n in (1)”改为“if n in [‘1’]”
在这里插入图片描述

n=input("要转换的温度选项;1,摄氏度转华氏度;2,华氏度转摄氏度:")
if n in ['1']:
    S=input("摄氏度为:")
    H=1.8*float(S)+32
    print("华氏度为:",format(H))
elif n in ['2']:
    H=input("华氏度为:") 
    S=(float(H)-32)/1.8
    print("摄氏度为:",format(S))


猜你喜欢

转载自blog.csdn.net/qq_44918057/article/details/128919214
今日推荐