使用Python模块
我正在尝试导入学生。Py模块并将其称为另一个,以在该模块中打印出词典的值,但它不起作用。我要去哪里?
这是学生。
def person1():
student1 = {
"name": "Joe",
"major": "Programming",
"format": "Campus Live"
}
这是我希望它从
import students
x = students.person1("major")
y = students.person1("name")
print(x)
print(y)
我想打印编程和名称乔的专业的文件。它们是2个不同的.py文件。是Python的新手
I am trying to import the students.py module and call it in another to print out values of the dictionary in that module but it is not working. Where am I going wrong?
Here is students.py
def person1():
student1 = {
"name": "Joe",
"major": "Programming",
"format": "Campus Live"
}
Here is the file am calling that module from
import students
x = students.person1("major")
y = students.person1("name")
print(x)
print(y)
I would like it to print the major which is programming and name Joe. They are 2 different .py files remember. Am still new to python
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发布错误也很有帮助。
(我也是Python的新手)。
但是您的Person1()是一个函数,但没有回报值。
我认为,您要寻找的是一堂课。
此回报:
从这里改编:
https://www.www.www.w3schools.com/python/python/python/python/python_classes.asp 人们对细节有所了解,但是也许这可以帮助您找到正确的答案,正在寻找
编辑:
或者,如果您有预定义的学生列表,则可以选择(嵌套)词典:
最佳
基督教
It would be helpful, to also post the error.
(I am also new to python).
But your person1() is a function, but has no return value.
I think, what you're looking for, is a class.
this return:
Adapted from here:
https://www.w3schools.com/python/python_classes.asp
Maybe some other people go a bit more into detail, but maybe this helps you to find the correct answer, you're looking for
EDIT:
Or if you have a list of predefined students, you can go for a (nested) dictionary:
best
Christian
您正在将值传递给一个函数,但现在并没有真正参数。
另外,您正在尝试从呼叫时不会返回任何值的函数中获取任何值,因此您需要添加带有字典所需值的返回关键字。
因此,这是可以使您的功能按照您想要的代码
You are passing values to a function but it doesn't really take arguments right now.
Also you are trying to get any values from a function which doesn't return any values when you call it, so you need to add return keyword with needed value from a dictionary.
So here is the code that will make your function working as you want