Python 3给了我无法解决的错误

时间:2015-09-02 12:02:27

标签: python conditional

下面是代码:

import turtle
import math

def drawTree(segments,size):
    """
    :param segments: refers to number of extensions from the previous
    :param size: refers to the length of the initial extension
    """
    if (segments < 0.0 | size < 0.0):
        print("Invalid Input")
    elif (segments == 0 | size == 0):
        pass
    elif segments > 0:
        i = 1
        for i in range(6):
            turtle.down()
            turtle.forward(size)
            turtle.back(size)
            turtle.right(60)
            i -= 1
            if segments != 0:
                size *= (1/3)
                turtle.forward(size)
                turtle.back(size)
                segments -= 1
                drawTree(segments, size)
def main():
    drawTree(1, 200)
    input("Press ENTER to exit")

main()

继承错误列表:

Traceback (most recent call last):
  File "C:/Users/MATTHEW/PycharmProjects/snowflake/snowflake.py", line 36, in <module>
    main()
  File "C:/Users/MATTHEW/PycharmProjects/snowflake/snowflake.py", line 33, in main
    drawTree(1, 200)
  File "C:/Users/MATTHEW/PycharmProjects/snowflake/snowflake.py", line 14, in drawTree
    if (segments < 0.0 | size < 0.0):
TypeError: unsupported operand type(s) for |: 'float' and 'int'

这些错误实际上并没有解释。实际上,这段代码运行正常,直到我添加了最后一个for循环,并且由于某种原因,它停止了。

1 个答案:

答案 0 :(得分:5)

您使用的操作符错误。对于布尔OR,请使用or operator

if (segments < 0.0 or size < 0.0):

elif (segments == 0 or size == 0):

|运算符是bitwise operator;它将布尔逻辑应用于整数的各个位。