2015-11-27 64 views
0

提供了一个程序来模拟生命在地球上以最简单的形式如何开始;数组中较大的数字(物种[])在数组旁边吃一个较小的数字,并使该数字变小。我做了一个if语句,它说明如果num> 0 & length> 1,那么做任何事情,其中​​num是程序运行的周期数,length是数组species []的长度。我已经测试了两个变量,它们都大于1.即使如此,if语句没有运行,其他语句也使得它跳出while循环。任何想法如何解决它?请以简单的方式解释,我刚刚开始编码。继承人我的代码:即使条件为真,python语句也不会运行

# Import Packages 
import random 

# Author 
__author__ = 'VectorImage' 

# Defaults 
print('DEFAULTS') 
print('Starting Size = 10') 
print('Chance Of New Species = 1') 
print('New Species Size = 5') 
print('Number of Cycles = 100') 
print('\n\n'); 


# Variables 
print('SET VARIABLES') 
choice = input('CUSTOM or DEFAULT: ') 
p3 = 11 

while p3 > 10: 
    if choice == 'CUSTOM': 
     p1 = int(input('Starting Size: ')) 
     p2 = int(input('Chance Of New Species (lower number means higher chance): '))-1 
     p3 = int(input('New Species Size: '))-1 
     p4 = int(input('Number of Cycles: ')) 
    elif choice != 'CUSTOM': 
     p1 = 10 
     p2 = 0 
     p3 = 5 
     p4 = 100 
    else: 
     print('species size cannot be more than x10') 
species = [p1, p1, p1] 
length = None 
l = None 
new_species = None 
chance = None 
num_range = None 
temp_num = None 
num = None 
print('\n\n') 


# Program 
def main(): 
    print('PROGRAM') 
    length = len(species) 
    if length > 2: 
     l = 0 
     num = p4 
     while 1 < 2: 
      print(species) 
      if num > 0 & length > 1: 
       length = len(species) 
       num_range = int(round(random.random()*(p3+1))) 
       new_species = int(round(random.random()*p2)) 
       chance = int(round(random.random())) 
       if new_species == 0: 
        if chance == 0: 
         species.insert(len(species) + num_range, length) 
        else: 
         species.insert(len(species) - num_range, length) 
       l += 1 
       num -= 1 
       print('Cycle #', p4-num) 
       print(length, ' species') 
      else: 
       break 
      if species[length-1] > species[length-2]: 
       temp_num = species[length-1] - num_range * (1 + p3) 
       species[length-2] -= temp_num 
       species[length-1] += temp_num 
      else: 
       temp_num = species[length-1] - (num_range * (1 + p3)) 
       species[length-1] += temp_num 
       species[length-2] -= temp_num 
      if species[length-1] <= 0: 
       del species[length-1] 
      elif species[length-2] <= 0: 
       del species[length-2] 

# RUN 
main() 
+1

'如果num> 0&length> 1'是一个按位并且 - 你想要一个逻辑和(用'和'表示) – UnholySheep

+0

'&!=和'在python中 –

回答

3

更换

if num > 0 & length > 1: 

if num > 0 and length > 1: 

在Python &是按位与和代码前行相当于if num > (0 & length) > 1:,这是不是你想要的。

+0

谢谢!它的工作......:D这实际上是我的科学公平项目。在七年级,希望我赢了!虽然谢谢! :) – Vansh03

1

&是不是你正在寻找的运营商。它比较两个操作数的位数。使用布尔运算符andor

相关问题