2014-01-06 2 views
0

Im нового для питона и я в настоящее время работаем над питоным скриптом и хотят добавить петлю в конце его, в настоящее время код выглядит следующим образом:Loop Python Script

#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 хотел узнать если бы можно было установить цикл, чтобы можно было ввести другого ученика, возможно ли это?

Спасибо

ответ

3

Почему бы не положить его в бесконечный цикл?

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: ') 

Или, если вы хотите сохранить все результаты:

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: ') 

И вы можете просто цикл по результатам, чтобы увидеть их.

+0

Возможно, вы захотите добавить опцию 'quit' :) –

+0

@ C.B. Добавлено, спасибо! – aIKid

+0

Спасибо! Петля работает правильно, однако, если я вхожу в «n», он все же решает снова зацикливаться, любое решение для этого? @alKid – Oscar

0

Вы имеете в виду еще одного ученика, который нужно ввести?

Возможно, вы можете сделать это через некоторое время или для цикла. Что-то в этих строках:

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