2017-11-09 17 views
-1

のコードしようとして助けが必要ですし、私は次のPython 3のコードにしようとしています:そして、それはすべての4人の学生のためにこれを行います後は、コーディングに私は非常に新しいです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. 

、プログラムがに持っていますsay:最高得点は(Student Name)で、平均得点は(Student score)です。

これはこれまで私が行ってきたことです。上記のように学生情報を個別にループさせるにはどうすればよいですか?私のものが次々に出てきます。

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

Plsは、修正IDLEで実行するか、あなたがLinux上である場合、たとえばpython3.6 ./grades.pyのために、ターミナルで実行します役職。それはあまり理解できません。 – Abend

+0

申し訳ありませんが、貼り付けたときにちょうどこのようになりましたが、どうすれば修正できますか? – Jules

+0

クイック検索は、書式設定に役立ちます:[スタックオーバーフローのコードをフォーマットする方法](https://duckduckgo.com/?q=How+to+format+code+in+Stack+Overflow%3F) – halfer

答えて

0

ここに解決策があります。 (正しい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)) 

いけない:)答えを受け入れることを忘れ

関連する問題