python温度转换输入一个摄氏温度为什么运行不了_Python温度转换器的if语句问题...

在Python代码中,作者遇到了一个温度转换器的问题,即程序始终将输入的温度视为摄氏度进行转换,不论用户指定的单位是华氏度还是开尔文。当用户输入非摄氏度单位时,程序未能正确执行相应的转换。作者怀疑可能需要创建独立的函数来处理不同单位的转换,但对Python不熟悉,寻求解决方案。
摘要由CSDN通过智能技术生成

base_temperature = raw_input("Temperature to convert: ")

temp = int(base_temperature)

base_unit = raw_input("Current unit of measure (Please choose Celsius, Fahrenheit, or Kelvin): ")

base_unit = base_unit.lower()

if base_unit.lower() == "celsius" or "c":

celsius = temp

fahrenheit = celsius * 9/5 + 32

kelvin = celsius + 273.15

print "%s in Celsius is %s in Fahrenheit and %s in Kelvin." % (celsius, fahrenheit, kelvin)

elif base_unit.lower() == "kelvin" or "k":

kelvin = temp

fahrenheit = kelvin * 9/5 - 459.67

celsius = kelvin - 273.15

print "%s in Kelvin is %s in Fahrenheit and %s in Celsius." % (kelvin, fahrenheit, celsius)

elif base_unit.lower() == "fahrenheit" or "f":

fahrenheit = temp

celsius = (fahrenheit - 32) * 5/9

kelvin = (fahrenheit + 459.67) * 5/9

print "%s in Fahrenheit is %s in Celsius and %s in Kelvin." % (fahrenheit, celsius, kelvin)

以上是我到目前为止在温度转换器上的代码,但我的问题是,它似乎“忽略”了基本上所有的代码,除了接受基本温度,然后将其从摄氏度转换为华氏度和开尔文。即使当我输入华氏度或开尔文作为基本单位时,它似乎忽略了这一点,只需通过“如果基本单位等于摄氏度”的代码,而不管输入到基本单位是什么。举个例子,如果我把100作为基准温度,用“华氏度”来表示基本单位,它会返回“100摄氏度等于212华氏度,373.15开尔文。我对Python非常陌生,所以我不太确定如何解决这个问题,我是否需要创建一个实际函数来分别从摄氏度、华氏度或开尔文进行转换?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值