[英]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. 我是python 3的新手,但是我正在制作一个简单的程序,可以选择一个学生的班级,并选择根据平均数,最高分等显示他们的分数。我想知道的是我可以返回代码的开头,因此我可以选择另一个类并选择另一组信息,例如。 Class B-average score.I would like to know how I could return to the start of the code.
B级平均分数。我想知道如何返回代码的开头。 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]
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). Python中普遍接受的方式是使用
while True:
无限期循环,然后在您要退出循环并结束程序(或继续放置在循环外的代码)时使用break
。
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. if code_should_end
替换为要触发结束的内容,无论是用户输入还是其他内容。 It also can be anywhere in your loop, it doesn't need to just be at the end. 它也可以在循环中的任何位置,而不必在最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.