Python Can';t在打印功能中运行列表
我是python的初学者。我已经编写了这段代码,但无法在print函数中执行for循环来遍历标记列表:Python Can';t在打印功能中运行列表,python,Python,我是python的初学者。我已经编写了这段代码,但无法在print函数中执行for循环来遍历标记列表: class student: def __init__(self, name, age, iden, lMark): self.name=name self.age=age self.iden=iden self.lMark=lMark def printAve(self, obj): for i
class student:
def __init__(self, name, age, iden, lMark):
self.name=name
self.age=age
self.iden=iden
self.lMark=lMark
def printAve(self, obj):
for i in obj.lMark:
res+=i
av=res/len(lMark)
return av
def disInfo(self, obj):
print("the name is: ",obj.name)
print("the age is: ",obj.age)
print("the identity is: ",obj.iden)
print("the marks are: ", for i in lMark:
print(i))
def addStudent(self, n, a, i, l):
ob=student(n,a,i,l)
ls.append(ob)
return true
def searchStudent(self, _val):
for i in len(ls):
if ls[i]==_val:
return i
ls=[]
s=student("Paul", 23,14, list(15,16,17,20,12))
bool added = s.add("Van", 20, 12, list(12,18,14,16))
if added==True:
print("Student added successfully")
for i in range(ls.__len__())
s.disInfo(ls[i])
有人能帮我解决这个问题并告诉我怎么做吗?你不能在参数列表中将
for
循环放在print
中,当然嵌入的print
不会返回它所打印的内容。你能做的就是
print(“标记为:”、“、”.join(lMark))
或许
print(“标记为:”,*lMark)
(另外,我相信你的意思是obj.lMark
)
重申一下,如果您将参数中的某些代码调用为print
,则该代码的计算结果应为您希望print
生成的文本;不要自己打印。比如说,
def pi()
返回3.141592653589793#未打印(3.141592653589793)
def形容词(arg):
如果arg>100:
返回“大”#不打印(“大”)
其他:
返回“小”#不打印(“小”)
打印(str(pi()),“is”,形容词(pi())
注意每个函数调用如何返回某些内容,调用方如何使用返回的值执行某些操作。尝试将其设置为函数。i、 e
def quantity():
for i in lMark:
print(i)
那么
首先,我回答你最初的问题,你可以用不同的方式打印一个列表,这里有一些。
您可以使用for循环对其进行迭代,并在不同的行上打印每个元素:
对于self.lMark中的元素:
打印(elem)
您还可以将所有值附加到字符串中,并用字符或其他方式分隔它们。
(注意:末尾将有一个尾随空格。)
myString=“”
对于self.lMark中的元素:
myString=myString+str(elem)+“
打印(myString)
更好的方法是使用字符串连接方法和容器迭代的简短版本:
“,”.join([str(i)表示self.lMark中的i])
代码示例中还有一些问题。
以下是脚本的运行版本:
班级学生:
定义初始(自我、姓名、年龄、身份、标记):
self.name=name
自我。年龄=年龄
self.iden=iden
self.lMark=lMark
def printAve(自、obj):
对于obj.lMark中的i:
res+=i
av=res/len(self.lMark)
返回av
def disInfo(self):
打印(“名称为:”,self.name)
打印(“年龄是:”,self.age)
打印(“标识为:”,self.iden)
打印(“标记为:”、“、”.join([str(i)表示self.lMark中的i]))
班级学生名单:
定义初始化(自):
自我。学生名单=[]
def添加(自身):
自我。学生名单。附加
返回真值
def get(自我):
返回自我。学生列表
def搜索(自我,姓名):
对于自学成才的学生名单:
如果s.name==名称:
返回s
一无所获
ls=学生名单()
s=学生(“保罗”,23,14,[15,16,17,20,12])
ls.添加(s)
添加=ls.添加(学生(“Van”,20,12,[12,18,14,16]))
如果添加:
打印(“学生添加成功”)
搜索\u name1=“Paula”
搜索\u name2=“Van”
如果ls.search(search\u name1):
打印(搜索名称1,“是列表的一部分!”)
其他:
打印(“没有”,搜索名称1)
如果ls.search(search_name2):
打印(搜索名称2,“是列表的一部分!”)
其他:
打印(“没有”,搜索名称2)
对于ls.get()中的学生:
student.disInfo()
打印(“-”*10)
我建议将学生名单和学生分成两个不同的班级,如上面的代码所示。您可以使用*
操作符解压缩您的名单:
print("the marks are: ", *lMark)
供参考,获取名为ls
的列表长度的典型方法是len(ls)
而不是ls.\uu len\uuuu()
。有关更多信息,请参见打印函数中的For i in lMark的问题,该代码甚至无法编译。这个网站不是一个帮助你解决这些基本语法问题的地方,在你询问具体的逻辑问题之前,应该先解决这些问题。这里有很多问题,其中任何一个都可能导致你含糊其辞地说的是错的。获得一个像样的IDE,它将在源代码中正确地显示错误,然后逐个解决每个问题。如果您不知道如何解决IDE报告的特定错误,请将其发布在此处,我们将帮助您解决。我的评论就是这样,一个评论。这不是对你问题的回答。这段代码有很多问题,确实需要重写。但在此之前,我建议先学习如何使用类,例如:这两个类在OPs代码中都不起作用,因为lMark(s)
是未定义的。-我很好奇,[I for I in lMarks]
做什么?这和图像标记不一样吗?你说得对-我想演示一下列表理解,但它太复杂了。谢谢你的反馈。
from statistics import mean
# classes are named LikeThis
# constants named LIKE_THIS
# other variables should be named like_this
# hungarian notation is not used in Python
class Student:
# instead of a global variable named ls,
# I'm using a class variable with a clear name instead
all_students = []
def __init__(self, name, age, iden, marks):
self.name = name
self.age = age
self.iden = iden
self.marks = marks
def printAve(self):
# you don't need to pass two arguments when you're only operating on a single object
# you don't need to reinvent the wheel, see the import statement above
return mean(self.marks)
def disInfo(self):
print("the name is:", self.name)
print("the age is:", self.age)
print("the identity is:", self.iden)
# you can't put a statement inside of an expression
# I'm guessing you want the marks all on the same line?
# the *args notation can pass any iterable as multiple arguments
print("the marks are:", *self.marks)
# this makes more sense as a classmethod
# clear variable names are important!
@classmethod
def addStudent(cls, name, age, iden, marks):
# I'm using cls instead of Student here, so you can subclass Student if you so desire
# (for example HonorStudent), and then HonorStudent.addStudent would create an HonerStudent
# instead of a plain Student object
cls.all_students.append(cls(name, age, iden, marks))
# note the capital letter!
return True
# again, this should be a classmethod
@classmethod
def searchStudent(cls, student):
# use standard methods!
try:
return cls.all_students.index(student)
except ValueError:
return None
# the literal syntax for lists in Python is `[1, 2, 3]`, _not_ `list(1, 2, 3)`.
# it also makes more sense to use Student.addStudent here, because just calling Student() doesn't add it
# to the list (although you could do that in __init__ if you always want to add them to the list)
Student.addStudent("Paul", 23, 14, [15, 16, 17, 20, 12])
# in Python, type annotations are optional, and don't look like they do in C or Java
# you also used `add` instead of `addStudent` here!
added: bool = Student.addStudent("Van", 20, 12, [12,18,14,16])
# don't need == True, that's redundant for a boolean value
if added:
print("Student added successfully")
# never call `x.__len__()` instead of `len(x)`
# (almost) never use `for i in range(len(x))` instead of `for item in x`
for student in Student.all_students:
student.disInfo()
print("the marks are: ", *lMark)