热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python位置参数错误_python–函数缺少2个必需的位置参数:’x’和’y’

我正在尝试编写一个绘制Spirograph的Python龟程序,我不断收到此错误:Traceback(mostrecentcalllast):FileC:\User

我正在尝试编写一个绘制Spirograph的Python龟程序,我不断收到此错误:

Traceback (most recent call last):

File "C:\Users\matt\Downloads\spirograph.py", line 36, in

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'

>>>

这是代码:

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

我知道这可能有一个简单的解决方案,但我真的无法弄清楚我做错了什么,这是我的计算机科学1课的练习,我不知道如何修复错误.

解决方法:

回溯的最后一行告诉您问题所在&#xff1a;

File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph

spirograph(p-1, x,y) #

TypeError: spirograph() missing 2 required positional arguments: &#39;x&#39; and &#39;y&#39;

在你的代码中,spirograph()函数有5个参数&#xff1a;def spirograph(R,r,p,x,y),它们是R,r,p,x,y.在错误消息中突出显示的行中,您只传递三个参数p-1,x,y,并且由于这与函数所期望的不匹配,因此Python会引发错误.

我还注意到你正在覆盖函数体中的一些参数&#xff1a;

def spirograph(R,r,p,x,y):

R&#61;100 # this will cancel out whatever the user passes in as &#96;R&#96;

r&#61;4 # same here for the value of &#96;r&#96;

t&#61;2*pi

这是一个简单的例子&#xff1a;

>>> def example(a, b, c&#61;100):

... a &#61; 1 # notice here I am assigning &#39;a&#39;

... b &#61; 2 # and here the value of &#39;b&#39; 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)

由于您始终希望将此值保留为默认值,因此您可以从函数的签名中删除这些参数&#xff1a;

def spirograph(p,x,y):

# ... the rest of your code

或者,您可以给他们一些默认值&#xff1a;

def spirograph(p,x,y,R&#61;100,r&#61;4):

# ... the rest of your code

由于这是一个分配,其余由你决定.

标签&#xff1a;turtle-graphics,python

来源&#xff1a; https://codeday.me/bug/20190728/1565651.html



推荐阅读
author-avatar
violet
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有