【问题标题】:if statement in python won't run even though conditions are true [duplicate]即使条件为真,python中的if语句也不会运行[重复]
【发布时间】:2015-11-27 22:58:12
【问题描述】:

制作了一个程序,以最简单的形式模拟地球上的生命是如何开始的;数组中较大的数字 (species[]) 在数组中吃掉它旁边的较小数字并使该数字更小。我做了一个 if 语句,如果 num > 0 & length > 1,然后执行任何操作,其中 num 是程序运行的周期数,length 是数组的长度,species[]。我已经测试了这两个变量,它们都大于 1。即便如此,if 语句没有运行,else 使它脱离了 while 循环。关于如何解决它的任何想法?请简单解释一下,我最近才开始编码。这是我的代码:

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

【问题讨论】:

  • if num &gt; 0 &amp; length &gt; 1 是按位和 - 你想要一个逻辑和(表示为 and
  • &amp; != and 在 python 中

标签: python arrays if-statement


【解决方案1】:

替换

if num > 0 & length > 1:

if num > 0 and length > 1:

Python中的&amp;是按位与,前一行代码等价于if num &gt; (0 &amp; length) &gt; 1:,这不是你想要的。

【讨论】:

  • 谢谢!它起作用了... :D 这实际上是我的科学博览会项目。七年级,希望我赢!不过谢谢! :)
【解决方案2】:

&amp; 不是您要查找的运算符。它比较两个操作数的位。使用布尔运算符andor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多