Advertisement
earlution

Grade calculator - decomposed

Oct 2nd, 2023 (edited)
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. # Separate function to calculate the grade
  2. def calculate_grade(student_score):
  3.     if student_score >= 60:
  4.         if student_score < 70:
  5.             return "D"
  6.         elif student_score < 80:
  7.             return "C"
  8.         elif student_score < 90:
  9.             return "B"
  10.         else:
  11.             return "A"
  12.     else:
  13.         return "F"
  14.  
  15.  
  16. # Separate function to get student information
  17. def get_student_info():
  18.     student_name = input("Enter student name (or type 'exit' to quit): ")
  19.  
  20.     if student_name.lower() == 'exit':
  21.         return None, None
  22.  
  23.     student_score = int(input("Enter student score: "))
  24.     return student_name, student_score
  25.  
  26.  
  27. # Separate function to display the grade
  28. def display_grade(student_name, student_score, student_grade):
  29.     print(f"Student {student_name} scored {student_score} and received a grade of {student_grade}.")
  30.  
  31.  
  32. # Main program
  33. def main():
  34.     print("Welcome to the Student Grade Calculator!")
  35.  
  36.     while True:
  37.         student_name, student_score = get_student_info()
  38.  
  39.         if student_name is None:
  40.             print("Goodbye!")
  41.             break
  42.  
  43.         student_grade = calculate_grade(student_score)
  44.         display_grade(student_name, student_score, student_grade)
  45.  
  46. if __name__ == "__main__":
  47.     main()
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement