-2

这是我的代码

def time_to_end_of_world(p_spread,p_cure):
    ...:     my_world=set_up_cities()
    ...:     zombify(my_world,0)
    ...:     count=0
    ...:     while is_end_of_world(my_world)==False:
    ...:         sim_step(my_world,p_spread,p_cure)
    ...:         count+=1
    ...:         return count

我不断得到 1 作为我的出局

如果我将返回计数移出像这样的缩进

def time_to_end_of_world(p_spread,p_cure):
    ...:     my_world=set_up_cities()
    ...:     zombify(my_world,0)
    ...:     count=0
    ...:     while is_end_of_world(my_world)==False:
    ...:         sim_step(my_world,p_spread,p_cure)
    ...:         count+=1
    ...:     return count

程序崩溃...

有任何想法吗?

4

4 回答 4

1

很有可能有人会从今年开始关注这个,所以......

我正在做与您相同的任务,并且遇到了相同的问题-但是我找到了解决方法。问题在于 sim_step() 中的 numpy.random.randint()。

随机整数函数不包括括号中的整数,因此您的循环永远不会结束,因为它永远不会感染 City 15;如果您使用 numpy.random.randint(15),最多只能达到 14 个。更改为 (16),它应该是固定的。

干杯

于 2016-10-10T19:14:56.580 回答
0

In the first example, you ALWAYS return the first time you enter the loop.

By moving the return statement outside the loop, you wait for is_end_of_world(my_world)==False to become False to exit the loop. It probably never does, leading to your program crashing.

于 2013-10-22T23:22:00.257 回答
0

The first version runs just one step of the simulation. The second attempts to run the simulation until the world ends. So something is going wrong in your sim_step or is_end_or_world code sometime in the steps after the first one.

于 2013-10-22T23:22:26.510 回答
0

the problem must be in the sim_step not actually updating my_world, or is_end_of_world is not ever returning true. the return is supposed to be "not indented" like the second example. post code for sim step and is_end_of_world

于 2013-10-22T23:23:25.797 回答