在Python中循环创建对象

时间:2015-05-24 23:21:22

标签: python class oop loops object

这是我的第一篇文章,所以如果我的格式错误,我很抱歉。

我之前在stackoverflow中搜索过它,它确实得到了一些响应,但我并没有真正理解,因为我无法与代码相关。我正在尝试练习面向对象的编程,我想要做的是我有一个Student类,只要用户不输入-1,我就有一个我想要循环的菜单。现在当用户按下1时,我想要创建一个新对象,但每次菜单循环循环时,我都想要一个新对象。我认为可以工作的是为主函数添加一个while循环计数器,每次用户输入" 1"时,计数器都会增加1.在创建对象时,它看起来是:student = Studentcounter 。我不是很确定,如果有很多其他帖子,我很抱歉,但我是新手哈哈。

class Student:
#name = ""      Do i need to put there variables here?
#age = 0
#science = 0
#maths = 0
#english = 0
def __init__(self,name,age,science,maths,english):
    self.name = name
    self.age = age
    self.science = science
    self.maths = maths
    self.english = english
    print(self.name)
    print(self.age)
    print(self.science)
    print(self.maths)
    print(self.english)

def mainFunct():
dictStudent = {}
mainBool = True
while mainBool == True:
    print("WElcome to rafs student grade book a")
    #while True:
        #try:
    menu = int(input("1) view students in specific class 2) Delete Student 3) Highest to lowest grades 4) Change student to a new class 5) add new marks 6) Change"))
            #break
        #except ValueError:
                  #print("Please enter in a number")

    if menu == 1:
        name = input("What is their name")
        age = input("What is their age")
    while True:
        science = int(input("What was their science result out of 100"))
        if science <= 100:
            break
    while True:
        maths = int(input("What was their maths result out of 100"))
        if maths <= 100:
            break
    while True:
        english = int(input("What was their english result out of 100"))
        if english <= 100:
            break
    student = Student(name,age,science,maths,english)




mainFunct()

0 个答案:

没有答案