【问题标题】:TypeError: '<' not supported between instances of 'float' and 'function'TypeError:'float'和'function'的实例之间不支持'<'
【发布时间】:2018-05-22 18:06:22
【问题描述】:

我正在尝试创建一个非常简单的病毒治愈模拟。我创建了两个文件(infection.py 和 Medicine.py),一个模拟病毒,一个模拟非常简单的治疗方法。第一个程序本身就可以很好地工作。它返回一个包含每个时间单位的病毒数量的列表。第二个程序导入了第一个程序,并且应该创建某种治疗方法。我主要使用程序 1 中的预定义函数,并重新定义了simulate()

要理解这个程序:'The Cure' 在timestep = 100 之后开始,这是在病毒被诊断出来之后。病毒只有在对治愈有抗药性的情况下才能繁殖,换句话说,如果病毒包含“AAA”。病毒可以死亡、繁殖和变异。突变有时会导致耐药性。

一切都应该正常工作(理论上?),但是在运行程序 2 时我一遍又一遍地收到相同的错误消息:TypeError: '&lt;' not supported between instances of 'float' and 'function'。我知道这是什么意思,但它说错误来自程序 1 中预定义的函数。但是程序 1 运行这个函数没有任何困难。我尝试存储random.random() 的值,然后使用它,但这不起作用。这是我的代码:

程序 1

import random

def generateVirus(length):
    return ''.join([random.choice(['A', 'G', 'T', 'C']) for i in range(length)])

def mutate(virus):
    rand = random.randint(0, len(virus)-1)
    return virus[:rand] + random.choice([i for i in 'AGTC' if i != virus[rand]]) + virus[rand+1:]

def kill(viruses, mortalityProb):
    return [survivors for survivors in viruses if random.random() > mortalityProb]

def reproduce(viruses, mutationProb, reproductionProb):
    nextgen = []
    for i in viruses:
        nextgen.append(i)
        if random.random() < reproductionProb:
            if random.random() < mutationProb:
                nextgen.append(mutate(i))
            else:
                nextgen.append(i)
    return nextgen

def reproductionProbability(viruses, maxReproductionProb, maxPopulation):
    return (1 - (len(viruses) / maxPopulation)) * maxReproductionProb

def simulate(viruses, mortalityProb, mutationProb, maxReproductionProb, 
    maxPopulation, timesteps = 500):
    pop_size = []
    while timesteps > -1:
        survivors = kill(viruses, mortalityProb)
        reproductionProb = reproductionProbability(survivors, maxReproductionProb, maxPopulation)
        viruses = reproduce(survivors, mutationProb, reproductionProb)
        pop_size.append(len(viruses))
        timesteps -= 1
    return pop_size

print(simulate(['GCTCC', 'CCGG', 'AACCGG', 'CCCTATAGG'], 0.05, 0.1, 0.07, 1000))

程序 2

import infection

def isResistent(virus):
    if virus.find('AAA') > -1:
        return True
    else:
        return False

def simulate(viruses, mortalityProb, mutationProb, maxReproductionProb, maxPopulation, timesteps = 500):
    activation_cure = 400
    while timesteps > -1:
        survivors = infection.kill(viruses, mortalityProb)
        for virus in viruses:
            if timesteps < activation_cure and isResistent(virus):
                reproductionProb = infection.reproductionProbability
                infection.reproduce(viruses, mutationProb, reproductionProb)
        timesteps -= 1
    return len(viruses)

def experiment(numberOfPatients):
    cured = 0
    viruses = []
    for i in range(10):
        viruses.append(infection.generateVirus(16))
    for i in range(numberOfPatients):
        remaining_virus = simulate(viruses, 0.05, 0.1, 0.07, 1000)
        if remaining_virus[len(remaining_virus)-1] == 0:
            cured += 1
    return cured

print(experiment(5))

完整的错误信息

File "C:\something\workspace\infection.py", line 17, in reproduce
if random.random() < reproductionProb:
TypeError: '<' not supported between instances of 'float' and 'function'

【问题讨论】:

  • infection.reproductionProbability 是您作为参数传递的函数。您的意思是调用函数并传递结果吗?
  • reproductionProb = infection.reproductionProbability in PROGRAM 2 是函数分配给可能应该是概率值的行。
  • 尝试在变量rand1=random.random() if random.random() &lt; reproductionProb:中捕获if条件之前的随机浮点值
  • 你是对的!谢谢你们俩帮助我! @彼得伍德

标签: python python-3.x


【解决方案1】:

错误信息很清楚:这行(第17行)是问题所在:

 if random.random() < reproductionProb

random.random() 是一个浮点数,reproductionProb 是一个函数句柄。您无法将浮点数与函数进行比较。

reproductionProb = infection.reproductionProbability

是您传递函数句柄而不是浮点数的调用。

您还需要确保将您的主要功能封装为:

if __name__=='__main__':

如果您导入文件,这会阻止调用导入文件的 main 函数。

【讨论】:

  • 你完全正确。我将那行代码更改为reproductionProb = infection.reproductionProbability(survivors, maxReproductionProb, maxPopulation),它现在可以工作了!感谢您的帮助!
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 2020-05-05
  • 2021-03-18
  • 2019-11-12
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
相关资源
最近更新 更多