我正在尝试练习类,对象,列表和循环。我正在努力制作一个学生成绩簿的程序。我将有一个菜单继续循环,当你按下1:它将让用户输入一个新的学生,他的年龄,科学成果,数学结果和英语结果。当用户输入姓名,年龄,科学结果,数学结果,英语结果时,我希望用户输入他们想要添加学生的学校课程(我有4个预设的学校班级):12A,12B,12C,12D和当用户输入其中一个类时,它会将学生的姓名附加到特定的学校课程。现在这里有一个问题:我希望能够添加新的学校课程,我也会把它放在菜单中。如果输入的值是例如,这是为什么我只能将if语句进行比较。 " 12A"但是我必须检查schoolClasses类中的每个值。
以下是代码:
#global variables
name = ""
allStudents = []
class Student:
name = ""
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 average(self):
self.Averagenum = (self.science + self.maths + self.english) / 3
print("The average is",self.Averagenum)
ef mainFunct():
global allStudents
workClass = []
global classes
dictStudent = []
mainBool = True
while mainBool == True:
print("WElcome to rafs student grade book a")
#while True:
#try:
menu = int(input("1) Add a new student to a 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)
allStudents.append(student)
while True:
whatClass = input("What class would you like to add a new student (12A,12B,12C,12D): ")
for i in range(len(classes)):
#if whatClass == classes[i]:
if classes.count(whatClass) == 0:
print("Need to make a new list then")
classes.append(whatClass)
print(classes)
break
mainFunct()
所以我真的不知道该怎么做我基本上我想要一个有学校班级数量的清单,然后我可以打电话给学校班级,看看学校班级里所有不同的学生。
从视觉上看,这可能类似于: 课程= 12A(&#34; Josh&#34;,&#34; Marvin&#34;)12B(&#34; Margaret&#34;,&#34; Tristan&#34;)
我还会在mainFunct()
中找到一些东西如果菜单== 3: addNewSchoolClass =输入(&#34;请输入您要添加的新学校类&#34;)..
很抱歉,如果我解释得非常糟糕,因为我在python上仍然非常糟糕。任何帮助都会非常感激,因为我一直在努力解决这个问题。
提前致谢!
答案 0 :(得分:1)
我使用Python的一些糖语法改进了你的程序。我试图应用最佳实践:
class Student(object):
def __init__(self, name, age, science, maths, english):
self.name = name
self.age = age
self.science = science
self.maths = maths
self.english = english
@property
def average(self):
return (self.science + self.maths + self.english) / 3.0
def __str__(self):
return "Student %s, Average: %s" % (self.name, str(self.average))
def main():
in_program = True
students = []
classes = {"12A": [], "12B": [], "12C": []}
print("Welcome to rafs student grade book a")
while in_program:
try:
menu = int(raw_input(
"1) Add a new student to a class \n2) Delete Student \n3) Highest to lowest grades \n4) Change student to a new class \n5) add new marks \n6) Change\n"))
except ValueError:
print "Insert a number please"
continue
if menu == 1:
name = raw_input("What is their name: ")
age = raw_input("What is their age: ")
science_score = read_score("science", 100)
math_score = read_score("math", 100)
english_score = read_score("english", 100)
student = Student(name, age, science_score, math_score, english_score)
students.append(student)
existent_class, class_ = read_class(classes)
if existent_class:
class_.append(student)
else:
classes.update({class_: [student, ]})
if menu == 3:
list_sorted = sorted(students, key=lambda student_: student_.average)
print [str(item) for item in list_sorted]
def read_class(available_classes):
class_ = raw_input(
"What class would you like to add a new student %s: " % ' '.join([k for k, v in available_classes.iteritems()]))
if class_ in available_classes:
return True, available_classes[class_]
return False, class_
def read_score(subject, limit):
score = limit + 1
while score > limit:
try:
score = int(raw_input("What was their %s result out of 100: " % subject))
except ValueError:
print "Insert a number please"
continue
return score
main()
请添加任何其他信息以改善您的问题
显然这不是一个完整的答案,它的目的是展示如何使用Python,从中你可以想象对程序的任何改进。