使用Python模块

发布于 2025-02-09 03:38:22 字数 380 浏览 1 评论 0原文

我正在尝试导入学生。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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2025-02-16 03:38:22

发布错误也很有帮助。

(我也是Python的新手)。

但是您的Person1()是一个函数,但没有回报值。
我认为,您要寻找的是一堂课。

class person():
  def __init__(self, name, major, format_):
    self.name = name
    self.major = major
    self.format_ = format_

p1 = person("John", "Programming", "Campus Live")


print(p1.name)
print(p1.major)
print(p1.format_)

此回报:

John
Programming 
Campus Live

从这里改编:

https://www.www.www.w3schools.com/python/python/python/python/python_classes.asp 人们对细节有所了解,但是也许这可以帮助您找到正确的答案,正在寻找

编辑:

或者,如果您有预定义的学生列表,则可以选择(嵌套)词典:

students = {
  "student1" : {
  "name": "Joe",
  "major": "Programming",
  "format": "Campus Live"
  },
   "student2" : {
   "name" : "Jane",
   "major" : "Economics",
   "format" : "Offline Campus"}
}

print(students["student1"]["name"])
print(students["student1"]["major"])
print(students["student1"]["format"])

最佳
基督教

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.

class person():
  def __init__(self, name, major, format_):
    self.name = name
    self.major = major
    self.format_ = format_

p1 = person("John", "Programming", "Campus Live")


print(p1.name)
print(p1.major)
print(p1.format_)

this return:

John
Programming 
Campus Live

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:

students = {
  "student1" : {
  "name": "Joe",
  "major": "Programming",
  "format": "Campus Live"
  },
   "student2" : {
   "name" : "Jane",
   "major" : "Economics",
   "format" : "Offline Campus"}
}

print(students["student1"]["name"])
print(students["student1"]["major"])
print(students["student1"]["format"])

best
Christian

远昼 2025-02-16 03:38:22

您正在将值传递给一个函数,但现在并没有真正参数。
另外,您正在尝试从呼叫时不会返回任何值的函数中获取任何值,因此您需要添加带有字典所需值的返回关键字。

因此,这是可以使您的功能按照您想要的代码

def person1(name):
  student1 = {
  "name": "Joe",
  "major": "Programming",
  "format": "Campus Live"
  }
  return student1[name]

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

def person1(name):
  student1 = {
  "name": "Joe",
  "major": "Programming",
  "format": "Campus Live"
  }
  return student1[name]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文