在 Python 中使用 Turtle 的螺旋图

问题描述 投票:0回答:3

我正在尝试使用 Python 的海龟为呼吸描记器编写代码,但我不断收到一个奇怪的错误。
到目前为止,这是我的代码:

import turtle
from math import *


def formulaX(R, r, p, t):
    x = (R-r)*cos(t) - (r+p)*cos((R-r)/r*t)

def formulaY(R, r, p, t):
    y = (R-r)*sin(t) - (r+p)*sin((R-r)/r*t)

def t_iterating(R, r, p):
    t = 0 
    turtle.down()

    while t < 20*pi:
        t = t+0.01
        turtle.goto(formulaX(R, r, p, t), formulaY(R, r, p, t))
    turtle.up()


def main():
    R = int(input("The radius of the fixed circle: "))
    r = int(input("The radius of the moving circle: "))
    p = int(input("The offset of the pen point, between <10 - 100>: "))

    if p < 10 or p > 100:
        input("Incorrect value for p!")

    t_iterating(R, r, p)

    input("Hit enter to close...")

main()

由于某种原因,我不断收到以下错误:

Traceback (most recent call last):
  File "/Users/liammitchell/Desktop/Comp Sci/Spirograph/spirograph.py", line 34, in <module>
main()
  File "/Users/liammitchell/Desktop/Comp Sci/Spirograph/spirograph.py", line 30, in main
t_iterating(R, r, p)
  File "/Users/liammitchell/Desktop/Comp Sci/Spirograph/spirograph.py", line 18, in t_iterating
turtle.goto(formulaX(R, r, p, t), formulaY(R, r, p, t))
  File "<string>", line 1, in goto
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 1774, in goto
self._goto(Vec2D(*x))
TypeError: type object argument after * must be a sequence, not NoneType    

如何解决这个错误?

python python-3.x typeerror turtle-graphics nonetype
3个回答
1
投票

函数

formulaX
formulaY
隐式返回
None
。您必须从其中获取一些值,才能将其用于其他功能,例如
return

所以你想要以下内容:

t_iterating



0
投票
def formulaX(R, r, p, t): return (R-r)*cos(t) - (r+p)*cos((R-r)/r*t)


main()



0
投票

for i in range(100): main()

	
© www.soinside.com 2019 - 2024. All rights reserved.