循环Python脚本

时间:2014-01-06 15:45:46

标签: python loops

我是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))

我想知道是否可以设置一个循环以便输入另一个学生,这可能吗?

谢谢

2 个答案:

答案 0 :(得分: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: ')

你可以循环查看结果。

答案 1 :(得分:0)

你的意思是还要再输一个学生?

你可以用while或for循环来做到这一点。这些方面的东西:

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