Python 为什么我能';t将浮点数输入到t?

Python 为什么我能';t将浮点数输入到t?,python,numpy,range,Python,Numpy,Range,我必须对基本逻辑生长进行编码,稍后将用于肿瘤生长 我必须把浮点值放在t上,但它给了我一个错误,我认为这是因为这个范围只能取整数。但范围必须是t。有没有办法做到这一点 物流增长 import numpy as np import matplotlib.pyplot as plt import csv val = float(input("Enter your value: ")) r = .25 # growth rate / year ,|(birth/death rate) K = 100

我必须对基本逻辑生长进行编码,稍后将用于肿瘤生长

我必须把浮点值放在t上,但它给了我一个错误,我认为这是因为这个范围只能取整数。但范围必须是t。有没有办法做到这一点

物流增长

import numpy as np
import matplotlib.pyplot as plt
import csv

val = float(input("Enter your value: ")) 
r = .25 # growth rate / year ,|(birth/death rate)
K = 100 # carrying capacity
t=val
num = np.zeros(t+1)
num[0] = 1

for i in range(t):
    num[i+1] = num[i]+r*num[i]*(1-num[i]/K)
    row= (i+1,'\t\t',format(num[i], '.8f'))
    print (row)

    with open('plot.csv', 'a') as csvFile:
            writer = csv.writer(csvFile, delimiter=' ')
            writer.writerow(row)
            csvFile.close()

plt.plot(range(t+1),num, 'b')
plt.xlabel('Time')
plt.ylabel(' Cell Number')
plt.title('Logistic Growth')
plt.axvline(np.argmax(np.diff(num)),  color = 'k'  )

plt.show()

如果您处理的是存储在
浮点数中的整数,您可以将
t
转换为
int

>>> range(int(7.0))
[0, 1, 2, 3, 4, 5, 6]
如果确实需要浮点范围,请使用以下选项:

>>> np.arange(0.2, 6.4, 0.5)
array([ 0.2,  0.7,  1.2,  1.7,  2.2,  2.7,  3.2,  3.7,  4.2,  4.7,  5.2,
        5.7,  6.2])

一个范围怎么可能是一个浮点数?您知道浮点数并不精确,取决于机器实现hi@Z.Boz-请单击旁边的复选标记标记最有用的答案。如果您的问题没有得到回答,请评论以澄清。谢谢,祝你好运!