将列表元素传递给函数
Passing list element to function
我有一个我编写的模块,其中包含一个收集大量输入并将它们附加到列表的函数。该模块还包含一堆需要使用列表元素的已定义海龟函数。但是,我在 turtle 函数上遇到语法错误。这是给我错误的确切函数(其他海龟函数的写法类似):
def draw_circle(turtle, shape_info[5]):
turtle.circle(shape_info[5])
列表元素5是用户先前在第一个函数中输入的长度输入。我究竟做错了什么?
错误是这样的:
Traceback (most recent call last):
File "C:\Users\ebarr\OneDrive\Programming\MIS 3300\MIS 3300\hw6.py", line 6,
in <module>
import hw6util
File "C:\Users\ebarr\OneDrive\Programming\MIS 3300\MIS 3300\hw6util.py", line 122
def draw_circle(evan, shape_info[5]):
^
SyntaxError: invalid syntax
这不是一个有效的函数定义:
def draw_circle(turtle, shape_info[5]):
您可能想要的是:
def draw_circle(turtle, shape_info):
turtle.circle(shape_info[5])
或者这样:
def draw_circle(turtle, shape_info_5th):
turtle.circle(shape_info_5th)
… 然后用 shape_info[5]
代替 shape_info
.
调用它
您可能希望执行以下操作:
# the function takes the list element type as the argument
def draw_circle(turtle, info):
turtle.circle(info)
user_length = 5; # index of the length in shape_info
# we call the function using the indexed element into the list
draw_circle(turtle, shape_info[user_length])
我有一个我编写的模块,其中包含一个收集大量输入并将它们附加到列表的函数。该模块还包含一堆需要使用列表元素的已定义海龟函数。但是,我在 turtle 函数上遇到语法错误。这是给我错误的确切函数(其他海龟函数的写法类似):
def draw_circle(turtle, shape_info[5]):
turtle.circle(shape_info[5])
列表元素5是用户先前在第一个函数中输入的长度输入。我究竟做错了什么?
错误是这样的:
Traceback (most recent call last):
File "C:\Users\ebarr\OneDrive\Programming\MIS 3300\MIS 3300\hw6.py", line 6,
in <module>
import hw6util
File "C:\Users\ebarr\OneDrive\Programming\MIS 3300\MIS 3300\hw6util.py", line 122
def draw_circle(evan, shape_info[5]):
^
SyntaxError: invalid syntax
这不是一个有效的函数定义:
def draw_circle(turtle, shape_info[5]):
您可能想要的是:
def draw_circle(turtle, shape_info):
turtle.circle(shape_info[5])
或者这样:
def draw_circle(turtle, shape_info_5th):
turtle.circle(shape_info_5th)
… 然后用 shape_info[5]
代替 shape_info
.
您可能希望执行以下操作:
# the function takes the list element type as the argument
def draw_circle(turtle, info):
turtle.circle(info)
user_length = 5; # index of the length in shape_info
# we call the function using the indexed element into the list
draw_circle(turtle, shape_info[user_length])