NeptorSV
NeptorSV

Reputation: 5

python how to return to the start of the code

I'm fairly new to python 3 but I'm making a simple program where I can select the class of a pupil and choose to display their scores based on averages,highest score etc... what I would like to know is how I can return to the start of the code so I can select another class and chceck another set of information eg. Class B-average score.I would like to know how I could return to the start of the code. Thanks for your time I appreciate it.

Note I cut of part of the code

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

Example of the code running

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]

Upvotes: 0

Views: 648

Answers (1)

SuperBiasedMan
SuperBiasedMan

Reputation: 9969

The commonly accepted way in Python is to use while True: to loop indefinitely and then just use break when you want to exit the loop and end the program (or continue with code you place outside the loop).

while True:
    # Run code
    if code_should_end:
        break

Replace if code_should_end with what you want to trigger the end, whether it's user input or something else. It also can be anywhere in your loop, it doesn't need to just be at the end.

Upvotes: 3

Related Questions