[英]having trouble passing in a variable from one function to another in python
i wanted to pass in one variable i made in one function into another function in order to print it out and do stuff with it, however i'm having trouble trying to pass the variable i made in the first function into the second function i wanted to pass in one variable i made in one function into another function in order to print it out and do stuff with it, however i'm having trouble trying to pass the variable i made in the first function into the second function
courses = [{k: json_dict[k] for k in keep_keys}
for json_dict in specific_major]
return courses
the courses variable that is above is what i want to use in my next function.上面的课程变量是我想在我的下一个 function 中使用的变量。 i tried returning it and then making a new function and passing it in as a function.
我尝试返回它,然后制作一个新的 function 并将其作为 function 传递。
def classes_get(c):
print(c)
classes_get(courses)
i tried the following code above in jupyter notebook to see if it would print out but i always get this message "courses not defined" even though i run all the code blocks in order and the course variable prints out normally when i call the first function its in. is there a step i'm missing that isn't allowing me to use it in another function?我在 jupyter notebook 中尝试了上面的以下代码,看看它是否会打印出来,但即使我按顺序运行所有代码块并且当我调用第一个 function 时,课程变量也会正常打印出来,但我总是收到此消息“课程未定义”它在。我是否缺少一个步骤,不允许我在另一个 function 中使用它?
def get_departments():
umd_departments = requests.get("https://api.umd.io/v0/courses/departments")
umd_departments_list = umd_departments.json()
umd_departments_list2 = json.dumps(umd_departments_list, indent=1)
department_storage = [department['dept_id'] for department in umd_departments_list]
print(department_storage)
dept = input('what department are you in right now: ')
dept = dept.upper()
if dept not in department_storage:
raise ValueError("department doesn't exist")
else:
department_url = requests.get(f"https://api.umd.io/v0/courses?dept_id={dept}")
specific_major =department_url.json()
keep_keys = ["course_id", "name", "credits", "sections"]
courses = [{k: json_dict[k] for k in keep_keys}
for json_dict in specific_major]
return courses
edit: get_departments is the first function.编辑:get_departments 是第一个 function。 sorry for not including all of it
抱歉没有包括所有内容
another edit: this is what i tried so far另一个编辑:这是我迄今为止尝试过的
courses = get_departments()
def get_classes(courses):
print(courses)
get_classes(courses)
Use the returned value from first function and pass it to the other function.使用第一个 function 的返回值并将其传递给另一个 function。 If you are using jupyter notebook, ensure that the
get_department
function is defined / run before called classes_get
.如果您使用的是 jupyter notebook,请确保在调用
get_department
之前定义/运行classes_get
。
courses = get_departments()
classes_get(courses)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.