函数缺少 2 个必需的位置参数:“x"和“y" [英] Function missing 2 required positional arguments: 'x' and 'y'
问题描述
我正在尝试编写一个绘制螺旋图的 Python 海龟程序,但我不断收到此错误:
回溯(最近一次调用最后一次):文件C:\Users\matt\Downloads\spirograph.py",第 36 行,在 <module> 中.主要的()文件C:\Users\matt\Downloads\spirograph.py",第 16 行,在主目录中螺旋图(R,r,p,x,y)文件C:\Users\matt\Downloads\spirograph.py",第 27 行,在螺旋图中螺旋图(p-1,x,y)类型错误:spirograph() 缺少 2 个必需的位置参数:x"和y">>>
这是代码:
from 海龟进口 *从数学导入 *定义主():p= int(input("输入 p"))R=100r=4t=2*pix= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)螺旋图(R,r,p,x,y)def spirograph(R,r,p,x,y):R=100r=4t=2*pix= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)而p<100和p>10:转到(x,y)螺旋图(p-1,x,y)如果p<10或p>100:打印(无效的 p 值,请输入 10 到 100 之间的值")输入(按回车相当")再见()主要的()
我知道这可能有一个简单的解决方案,但我真的不知道我做错了什么,这是我计算机科学 1 课上的一个练习,我不知道如何修复错误.
回溯的最后一行告诉你问题出在哪里:
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirographspirograph(p-1, x,y) # <--- 这是问题所在类型错误:spirograph() 缺少 2 个必需的位置参数:x"和y"
在您的代码中,spirograph()
函数接受 5 个参数:def spirograph(R,r,p,x,y)
,它们是 R
、r
、p
、x
、y
.在错误消息中突出显示的行中,您只传递了三个参数 p-1, x, y
,由于这与函数预期的不匹配,Python 会引发错误.
我还注意到您覆盖了函数体中的一些参数:
def spirograph(R,r,p,x,y):R=100 # 这将抵消用户作为 `R` 传入的任何内容r=4 # `r` 的值在这里相同t=2*pi
以下是正在发生的事情的简单示例:
<预><代码>>>>定义示例(a,b,c=100):... a = 1 # 注意这里我指定了 'a'... b = 2 # 这里'b' 的值被覆盖... # c 的值默认设置为 100...打印(a,b,c)...>>>example(4,5) # 这里我为 a 传入 4,为 b 传入 5(1, 2, 100) # 但注意它没有任何效果>>>example(9,10,11) # 这里我传入的是 c 的值(1, 2, 11)由于您始终希望将此值保留为默认值,因此您可以从函数的签名中删除这些参数:
def spirograph(p,x,y):# ... 其余的代码
或者,您可以给它们一些默认值:
def spirograph(p,x,y,R=100,r=4):# ... 其余的代码
由于这是一项分配,其余的由您决定.
I am trying to write a Python turtle program that draws a Spirograph and I keep getting this error:
Traceback (most recent call last):
File "C:\Users\matt\Downloads\spirograph.py", line 36, in <module>
main()
File "C:\Users\matt\Downloads\spirograph.py", line 16, in main
spirograph(R,r,p,x,y)
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y)
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
>>>
This is the code:
from turtle import *
from math import *
def main():
p= int(input("enter p"))
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
spirograph(R,r,p,x,y)
def spirograph(R,r,p,x,y):
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
while p<100 and p>10:
goto(x,y)
spirograph(p-1, x,y)
if p<10 or p>100:
print("invalid p value, enter value between 10 nd 100")
input("hit enter to quite")
bye()
main()
I know this maybe has a simple solution but I really can't figure out what I am doing wrong, this was an exercise in my computer science 1 class and I have no idea how to fix the error.
The last line of the traceback tells you where the problem is:
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y) # <--- this is the problem line
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
In your code, the spirograph()
function takes 5 arguments: def spirograph(R,r,p,x,y)
, which are R
, r
, p
, x
, y
. In the line highlighted in the error message, you are only passing in three arguments p-1, x, y
, and since this doesn't match what the function is expecting, Python raises an error.
I also noticed that you are overwriting some of the arguments in the body of the function:
def spirograph(R,r,p,x,y):
R=100 # this will cancel out whatever the user passes in as `R`
r=4 # same here for the value of `r`
t=2*pi
Here is a simple example of what is happening:
>>> def example(a, b, c=100):
... a = 1 # notice here I am assigning 'a'
... b = 2 # and here the value of 'b' is being overwritten
... # The value of c is set to 100 by default
... print(a,b,c)
...
>>> example(4,5) # Here I am passing in 4 for a, and 5 for b
(1, 2, 100) # but notice its not taking any effect
>>> example(9,10,11) # Here I am passing in a value for c
(1, 2, 11)
Since you always want to keep this values as the default, you can either remove these arguments from your function's signature:
def spirograph(p,x,y):
# ... the rest of your code
Or, you can give them some defaults:
def spirograph(p,x,y,R=100,r=4):
# ... the rest of your code
As this is an assigment, the rest is up to you.
这篇关于函数缺少 2 个必需的位置参数:“x"和“y"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!