0% found this document useful (0 votes)
3 views5 pages

Python Loops

The document provides Python code examples for implementing a grading system using three types of loops: for, while, and a simulated do-while loop. Each loop allows users to input student scores and outputs corresponding grades based on predefined score thresholds. The document also explains how each loop functions and how to modify the number of students processed.

Uploaded by

AhmadNadeem Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Loops

The document provides Python code examples for implementing a grading system using three types of loops: for, while, and a simulated do-while loop. Each loop allows users to input student scores and outputs corresponding grades based on predefined score thresholds. The document also explains how each loop functions and how to modify the number of students processed.

Uploaded by

AhmadNadeem Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Using a for loop to process multiple students

num_students = 5 # You can change this number

for i in range(num_students):

score = int(input("Enter the student's score: "))

if score >= 80:

grade = 'A'

elif score >= 70:

grade = 'B'

elif score >= 50:

grade = 'C'

else:

grade = 'Fail'

print(f"Student's score: {score}, Grade: {grade}")

Here are the three types of loops (for, while, and while True) implementing the grading system:

1. Using for loop

num_students = 5 # Change this for more students

for i in range(num_students):

score = int(input("Enter the student's score: "))

if score >= 80:


grade = 'A'

elif score >= 70:

grade = 'B'

elif score >= 50:

grade = 'C'

else:

grade = 'Fail'

print(f"Student's score: {score}, Grade: {grade}")

2. Using while loop

num_students = 5 # Change this for more students

i=0

while i < num_students:

score = int(input("Enter the student's score: "))

if score >= 80:

grade = 'A'

elif score >= 70:

grade = 'B'

elif score >= 50:

grade = 'C'

else:

grade = 'Fail'
print(f"Student's score: {score}, Grade: {grade}")

i += 1 # Increment counter

Python doesn't have a built-in do-while loop like some other languages (e.g., C, C++), but you
can simulate it using a while True loop with a break condition. Here's how you can implement
the grading system using a do-while style loop in Python:

while True:

score = int(input("Enter the student's score (or -1 to exit): "))

if score == -1:

print("Exiting the grading system.")

break # Exit the loop if the user enters -1

if score >= 80:

grade = 'A'

elif score >= 70:

grade = 'B'

elif score >= 50:

grade = 'C'

else:

grade = 'Fail'

print(f"Student's score: {score}, Grade: {grade}")

How This Works Like a do-while Loop:

1. The loop runs at least once, even if the user enters -1 immediately.

2. The user enters a score first.

3. If the user inputs -1, the loop breaks and exits.


4. Otherwise, it checks conditions and prints the grade.

5. The process repeats until the user enters -1.

Let me know if you need any changes! 😊

TEACHER WALA

Sure! Here’s your code with a for loop, allowing multiple students to enter their scores:

num_students = int(input("Enter the number of students: ")) # Asking how many students

for i in range(num_students): # Loop runs for each student

score = int(input("Enter the student's score: "))

if score >= 80:

print("Grade: A")

elif score >= 70:

print("Grade: B")

elif score >= 50:

print("Grade: C")

else:

print("Grade: Fail")

How It Works:

1. Asks for the number of students (num_students).

2. Runs a for loop for each student.

3. Takes input and calculates the grade.

4. Repeats the process until all students have entered their scores.
Would you like me to modify anything? 😊

num_students = int(input("Enter the number of students: ")) # Asking for the number of
students

i = 0 # Counter for students

while i < num_students: # Loop runs until all students enter their scores

x = int(input("Enter your score: ")) # Taking input

if x >= 80:

print("Grade: A")

elif x >= 70:

print("Grade: B")

elif x >= 50:

print("Grade: C")

else:

print("Grade: Fail")

i += 1 # Increment counter

You might also like