0

Im new to python and I am currently working on a python script and want to add a loop at the end of it, currently the code is as follows:

#FinalGrade

print ("\n")
Institution = str(input("Please Enter the Name of Your insitution: "))
print ("\n")
Year = str(input("Please Enter the Year of the Student (For Example, 'Year 2'): "))
print ("\n")
Student = str(input("Student Full Name: "))
print ("\n")
Grade1 = int(input("Enter Student's First Term Grade: "))
Grade2 = int(input("Enter Student's Second Term Grade: "))
Grade3 = int(input("Enter Student's Third Term Grade: "))
Grade4 = int(input("Enter Student's Fourth Term Grade: "))

average =  (Grade1+Grade2+Grade3+Grade4)/4

print ("\n")
print ("Total Grade Average: %G" % (average))

passed_or_failed = "PASSED"
if average < 40:
   passed_or_failed = 'FAILED'

print ("\n")
print ("%s has: %s" % (Student, passed_or_failed))

Id like to find out if it would be possible to set a loop so another student can be entered, would this be possible?

Thank you

2 Answers 2

3

Why not put it in an infinite loop?

cont = 'y'
while cont=='y':
    print ("\n")
    Institution = str(input("Please Enter the Name of Your insitution: "))
    print ("\n")
    Year = str(input("Please Enter the Year of the Student (For Example, 'Year 2'): "))
    print ("\n")
    Student = str(input("Student Full Name: "))
    print ("\n")
    Grade1 = int(input("Enter Student's First Term Grade: "))
    Grade2 = int(input("Enter Student's Second Term Grade: "))
    Grade3 = int(input("Enter Student's Third Term Grade: "))
    Grade4 = int(input("Enter Student's Fourth Term Grade: "))

    average =  (Grade1+Grade2+Grade3+Grade4)/4

    ...
    cont = input('Do you want to keep entering students? y/n: ')

Or if you want to keep all of the results:

results = []
cont = 'y'

while cont=='y':
    print ("\n")
    Institution = str(input("Please Enter the Name of Your insitution: "))
    ...

    passed_or_failed = "PASSED"
    if average < 40:
       passed_or_failed = 'FAILED'
    results.append(passed_or_failed)
    ...
    cont = input('Do you want to keep entering students? y/n: ')

And you can just loop through the results to see them.

Sign up to request clarification or add additional context in comments.

3 Comments

Might want to add a quit option :)
Thank you! The loop works correctly however if I enter 'n' it still decides to loop again, any solution for this? @alKid
To you redefine cont anywhere? Put print(repr(cont)) beneath the while statement to see its value each time through the loop.
0

You mean just one more student to be entered?

You can probably do that with a while or for loop. Something along these lines:

counter = 0
while (counter < 2):
    your existing code
    ....
    counter += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.