我需要一些帮助。我试图让我的 for 循环使用小数,但我的代码不接受浮点数,我不确定下一步该做什么。谁能指出我哪里出错了?
它是用于将用户定义的步骤 (Delta) 中的摄氏温度转换为华氏温度的代码。这里是:
def main():
# Handshake
print("This program will convert a range of Celsius degrees to")
print("Fahrenheit degrees based on your input.")
# Ask and read low end of range
Rangelow = eval(input("Enter the low end of your range: "))
# Ask and read top end of range
Rangehigh = 1 + eval(input("Enter the high end of your range: "))
# Ask and read Delta
Delta = eval(input("Enter the Delta for your range: "))
#Display output
print("Celsius to Fahrenheit by", Delta)
for i in range(Rangelow, Rangehigh, Delta):
print(i, " ", 9/5 * i + 32)
main()
这是我的意思的一个例子:
该程序将根据您的输入将一系列摄氏度转换为华氏度。输入范围的低端:3.8 输入范围的高端:14.7 输入范围的 Delta:1.1 摄氏度到华氏度 1.1 回溯(最近调用最后一次):文件“C:\Users\jarre\Desktop\ Python Programs\Conversion.py”,第 27 行,在 main() 文件“C:\Users\jarre\Desktop\Python Programs\Conversion.py”中,第 22 行,在 main for i in range(Rangelow, Rangehigh + 1, Delta): TypeError: ‘float’ 对象不能解释为整数
我应该注意到问题似乎出在输入上,输出在输入转换后抛出小数点没有问题。
原文由 Jarbcd 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不能使用内置函数来进行浮点数/小数增量,但构建自己的生成器相当容易:
或者您可以使用
numpy
但这感觉就像是用大锤敲打螺母: