python如何返回代码的开头

时间:2015-09-07 14:38:29

标签: python python-3.x

我对python 3很新,但是我制作了一个简单的程序,我可以选择一个学生的班级,并选择根据平均分数,最高分等显示他们的分数......我是什么想知道我是如何回到代码的开头所以我可以选择另一个类并chceck另一组信息,例如。 B级平均分。我想知道如何返回代码的开头。谢谢你的时间,我很感激。

注意我剪切了部分代码

import csv
print("1 for Class A\n2 for Class B\n3 for Class C")
choosen=int(input())
class_a = open('class_a.txt')
class_b = open('class_b.txt')
class_c = open('class_c.txt')
if choosen == 1:
    print("1 for for alphabetical orderwith each students highest score\n2 for highest score, highest to lowest\n3 for average score, highest to lowest")
    cho_two=int(input())
    csv_a = csv.reader(class_a)
    a_list = []
    for row in csv_a:
        row[3] = int(row[3])
        row[4] = int(row[4])
        row[5] = int(row[5])
        minimum = min(row[3:5])
        row.append(minimum)
        maximum = max(row[3:5])
        row.append(maximum)
        average = sum(row[3:5])//3
        row.append(average)
        a_list.append(row[0:9])
    if cho_two == 1:
        alphabetical = [[x[0],x[6]] for x in a_list]
        print("\nCLASS A\nEach students highest by alphabetical order \n")
        for alpha_order in sorted(alphabetical):
            print(alpha_order)
            class_a.close()
    elif cho_two == 2:
        print("\nCLASS A\nThe highest score to the lowest \n")
        for high_scr in sorted(highest_score,reverse = True):
            print(high_scr)
            class_a.close()           
    elif cho_two == 3:
        average_score = [[x[8],x[0]] for x in a_list]
        print("\nCLASS A\nThe average score from highest to lowest \n")
        for ave_scr in sorted(average_score,reverse = True):
            print(ave_scr)
            class_a.close()

运行代码的示例

1 for Class A
2 for Class B
3 for Class C
1
1 for for alphabetical orderwith each students highest score
2 for highest score, highest to lowest
3 for average score, highest to lowest
1

CLASS A
Each students highest by alphabetical order 

['Bob', 2]
['Hamza', 6]
['James', 5]
['Jane', 0]
['John', 3]
['Kate', 3]

1 个答案:

答案 0 :(得分:3)

Python中普遍接受的方法是使用while True:无限循环,然后在想要退出循环并结束程序时使用break(或继续使用放在循环外的代码) )。

while True:
    # Run code
    if code_should_end:
        break

if code_should_end替换为您要触发结束的内容,无论是用户输入还是其他内容。它也可以在你的循环中的任何地方,它不需要只是在最后。