vendredi 27 novembre 2015

if statement in python won't run even though conditions are true

made a program to simulate how life began on Earth in it's simplest form; the bigger number in the array (species[]) eats a smaller number beside it in the array and makes that number smaller. I have made an if statement which says if num > 0 & length > 1, then do whatever where num is the number of cycles the program runs and length is the length of the array, species[]. I have tested both variables and they are both more than 1. Even still, the if statement isn't running, the else is making it break out of the while loop. Any ideas on how could fix it? please explain in a simple way, I just started coding recently. Heres my code:

# 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()

Aucun commentaire:

Enregistrer un commentaire