我对编码很新,我正在尝试在Python 3中编写以下代码:
How many students?: 4
How many units?: 3
What is the name of student 1?: John
What did John get in unit 1?: 34
34 out of 100 is a Fail.
What did John get in unit 2?: 67
67 out of 100 is a Credit.
What did John get in unit 3?: 52
52 out of 100 is a Pass.
On average, John is getting a Pass.
然后,对所有4名学生进行此操作后,该计划必须说:最高分为(学生姓名),平均分为(学生分数)。
这是我到目前为止所拥有的。如何让它像上面一样单独循环学生信息?我一个接一个地出来。
numStudent = int (input('Please enter number of students: '))
numUnit = int(input('Please enter the number of units for your course: '))
for i in range(numStudent):
studentName = input('Please enter students first name: ')
for j in range(numUnit):
unitmark = int(input('what did' + ' ' + studentName + ' ' + 'get for unit:'))
def calculateGrade(unitmark):
if unitmark <= 49:
Print(unitmark,"out of 100 You are failing")
elif unitmark <= 59:
return(unitmark,"out of 100 is a Pass")
elif unitmark <=69:
return(unitmark, "out of 100 is a Credit")
elif unitmark <= 79:
return(unitmark, "out of 100 is a Distinction")
elif unitmark <=100:
return(unitmark, "out of 100 is a High Distinction")
print(calculateGrade(unitmark))
totalGrade = numUnit + unitmark
averageGrade = totalGrade / 100
print(round (averageGrade, 7))
答案 0 :(得分:0)
这是解决方案。在IDLE中运行或者如果您在Linux上运行它在终端中运行,例如python3.6 ./grades.py
(指定正确的Python版本并创建grades.py)
def get_grade(score, name=None):
if score <= 49:
grade = "You are failing"
elif score <= 59:
grade = "is a Pass"
elif score <= 69:
grade = "is a Credit"
elif score <= 79:
grade = "is a Distinction"
elif score <= 100:
grade = "is a High Distinction"
if not name:
print(score, "out of 100", grade)
else:
print('On average,', name, grade)
num_students = int(input('How many students?: '))
num_units = int(input('How many units?: '))
students_db = {}
for student_num in range(1, num_students+1):
student_name = input('\nWhat is the name of student %s?: ' % student_num)
current_student_total_scores = 0
for unit_num in range(1, num_units+1):
unitmark = int(input('\nWhat did %s get in unit %s?: ' % (student_name, unit_num)))
current_student_total_scores += unitmark
get_grade(unitmark)
avarage_score = current_student_total_scores/num_units
get_grade(avarage_score, student_name)
students_db.update({student_name: avarage_score})
name, avg_score = max(students_db.items(), key = lambda p: p[1])
print('\nThe top score is %s with an average score of %s' % (name, avg_score))
别忘了接受答案:)