2014-01-06 67 views
0

进出口新的蟒蛇,我目前工作的一个python脚本,并希望在它的末尾添加一个循环,目前的代码如下:循环的Python脚本

#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)) 

我还想找出如果可以设置一个循环来让另一个学生进入,这可能吗?

谢谢

回答

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

可能要添加一个'退出'选项:) –

+0

@ C.B。补充,谢谢! – aIKid

+0

谢谢!循环工作正常,但是如果我输入'n'它仍然决定再次循环,任何解决方案? @alKid – Oscar

0

你是说只有一个学生进入?

你或许可以用一段时间或for循环做到这一点。沿着这些线:

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