【问题标题】:Program prints the else condition even when the if statements are true in Python即使 if 语句在 Python 中为真,程序也会打印 else 条件
【发布时间】:2021-09-23 09:22:47
【问题描述】:

我想将我的摄氏温度转换为华氏温度,反之亦然。这是我给出的代码:

temperature=int(input('温度是多少?'))

type=input('是摄氏度还是华氏度:')

如果温度 == '摄氏度':

converted_temp = (temperature*9/5)+32 

print("The converted temp in fahrenheitis: ",converted_temp) 

elif 温度 == '华氏度':

converted_temp = (temperature-32)*5/9 

print('The converted temperature in celsius is:',converted_temp) 

其他:

print("SORRY! Enter either Celsius or Fahrenheit") 

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python-3.x if-statement


【解决方案1】:

只是不要使用type这个词,它在python中注册为特殊词

temperature_type = input('is it in celsius or fahrenheit: ')
temperature_value = int(input('what is the temperature? '))

if temperature_type == 'celsius': 
    converted_temp = (temperature_value * 9 / 5) + 32 
    print("The converted temp in fahrenheitis: ", converted_temp) 
elif temperature_type == 'fahrenheit': 
    converted_temp = (temperature_value - 32) * 5 / 9 
    print('The converted temperature in celsius is:', converted_temp) 
else: 
    print("SORRY! Enter either Celsius or Fahrenheit")

【讨论】:

  • 感谢您的回答。我现在明白了。但是,您没有看到任何问题是什么意思。我在标题中发布了这个问题。现在是你发布的地方吗?我是堆栈溢出的新手。
  • @Juggle 抱歉,不明白这是个问题 :) 祝你有美好的一天
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多