python - 如何修复python中的变量“t”错误?
问题描述
我有这段代码,当我尝试转换为 farenheit 时出现错误。它基本上是一个 Windchill 计算器:
import math
c = ""
f = ""
t = 0
def temp (t):
t = (9/5 * temp_chosen) + 32
temp_chosen = float(input("What is the temperature? :"))
scale = input("Farenheit or Celsius (F/C)? ").upper()
def wind():
if scale == "C":
return (t)
print(t)
else:
t = temp_chosen
print(t)
for i in range (5, 65, 5):
wind_chill = 35.74 + (0.6215 * t) -35.75 * (i ** 0.16) + 0.4275 * ((t)) * (i ** 0.16)
print(f"At temperature {t}F, and wind speed {i} mph, the windchill is: {wind_chill:.2f}F")
temp (t)
wind ()
我得到这个错误:
Traceback (most recent call last):
File "c:/Users/Azevedo/Documents/Codes/test.py", line 28, in <module>
wind ()
File "c:/Users/Azevedo/Documents/Codes/test.py", line 14, in wind
return (t)
UnboundLocalError: local variable 't' referenced before assignment
我该如何解决?
解决方案
我刚刚清理了你的代码。另外,我修复了缩进。
import math
c,f = "",""
t = 0
def wind():
temp_chosen = float(input("What is the temperature? :"))
scale = input("Farenheit or Celsius (F/C)? ").upper()
t = (9/5 * temp_chosen) + 32
if scale == "C":
print(t)
else:
t = temp_chosen
for i in range (5, 65, 5):
wind_chill = 35.74 + (0.6215 * t) -35.75 * (i ** 0.16) + 0.4275 * ((t)) * (i ** 0.16)
print(f"At temperature {t}F, and wind speed {i} mph, the windchill is: {wind_chill:.2f}F")
if __name__ == "__main__":
wind()
推荐阅读
- acumatica - Acumatica - 引发多个错误/异常
- typescript - Sinon 存根函数不能与函数内部的函数一起使用
- flutter - Cubit State 不会在 BottomSheet 内刷新
- postgresql - PostgreSQL 中的外键过滤
- c# - 无法让 Identity Server 4 API 授权其自己的端点,而是发送登录页面
- python - 用于 KMeans 聚类每次迭代中的标签的 Scikit-learn api
- javascript - 用于 ECDH 输出的 Web 密码学实现 HKDF
- android - 如何在 kotlin 的帮助下从 Cloud Firestore 中删除 recyclerview 项目?
- powershell - powershell在计算前计算属性插入变量
- r - 使用 fct_infreq() 按频率排列带有字符的多列