Python - unable to get simple while loop to work (student project)

Viewed 49

I hope someone can help me with a school project.

The question is -

A university wants a print-out of how many students failed the first year mathematics final examination. Each record contains the student’s number, name, mathematics score and maximum score possible. Each student must have a report printed with his or her number, name, percentage and a remark stating whether a pass or fail is achieved (50% and above is a pass). Processing continues until a student number of 0 is entered. Count how many students failed, as well as the total number of students, and print these totals

I am trying to get the student number from user input to either loop or break out of the loop but I can't get it to work.

I've changed my code so many times but this is my last attempt -

total_students = 0
total_fail = 0

  while True:
  student_number = int(input("what is your student number? "))
  name = input("What is your name?")
  math_score = int(input("Math score?"))
  max_score = int(input("Max score? "))

  if student_number == 0:
      print(total_fail)
      print(total_students)

  if math_score / max_score * 100 >= 50:
        print("You passed")
        total_students += 1

  elif math_score / max_score * 100 < 50:
     print("You failed")
     total_fail += 1
     total_students += 1
2 Answers

The indentation for while loop is incorrect, and since you want the program to stop after student number is 0, youll have to use break. For more info on it check this out.

total_students = 0
total_fail = 0
while True:
  student_number = int(input("what is your student number? "))
  name = input("What is your name?")
  math_score = int(input("Math score?"))
  max_score = int(input("Max score? "))

  if student_number == 0:
      print(total_fail)
      print(total_students)
      break

  if math_score / max_score * 100 >= 50:
        print("You passed")
        total_students += 1

  elif math_score / max_score * 100 < 50:
     print("You failed")
     total_fail += 1
     total_students += 1

    

1- the while loop is infinite so you need to put break to end the loop when needed

2- your codes should be inside the while loop

notice :

while True:
    #code inside the loop 
Related