我创建了一个算术代码,询问用户10个问题,然后将他们的名字和分数存储在数据库中,例如C:\class1.txt
我现在处于舞台上,在那里我应该能够按照从最高到最低(分数)和按字母顺序对包含每个班级的多个学生的姓名和分数的文件进行排序。程序应该在代码末尾提出一个问题,询问教师是否希望按字母顺序排序,或者按分数从最高到最低排序。他们也应该能够选择他们想要分类的课程,并且应该打印出来。
我在这方面寻求指导,我不想作弊,我现在只是在这个阶段无能为力;和一位没用的老师。
提前致谢
import random
USER_SCORE = 0
questions = 0
classnumber = ("1","2","3")
name1= input("Enter Your Username: ")
print("Hello, " + name1)
print(" Welcome to the Arithmetic Quiz")
classno = input("What class are you in?")
while classno not in classnumber:
print("Enter a valid class")
print("ENTER ONLY THE NUMBER n\ 1 n\ 2 n\3")
classno=input("What class are you in?")
while questions <10:
for i in range(10):
num1=random.randint(1,10)
num2=random.randint(1,10)
on=random.choice("*-+")
multiply=num1*num2
subtract=num1-num2
addition=num1+num2
if on == "-": #If "-" or subtract is randomly picked.
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question" ,questions, "/10")
uinput=input(str(num1)+" - "+str(num2))
if uinput == str(subtract):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: " ,USER_SCORE,)
else:
print (" Incorrect, the answer is: " +str(subtract))
USER_SCORE+=0
if on == "+":
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question",questions, "/10")
uinput=input(str(num1)+" + "+str(num2))
if uinput == str(addition):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: ",USER_SCORE,)
else:
print(" Incorrect, the answer is: " +str(addition))
USER_SCORE+=0
if on == "*":
print("MAKE SURE YOU ENTER A NUMBER OTHERWISE YOU WILL BE MARKED DOWN")
questions+=1
print(" Question",questions, "/10")
uinput=input(str(num1)+" * "+str(num2))
if uinput == str(multiply):
USER_SCORE+=1
print(" Correct, your USER_SCORE is: " ,USER_SCORE,)
else:
print(" Incorrect, the answer is: " +str(multiply))
USER_SCORE+=0
if USER_SCORE >9:
print("Well done," ,name1, "your score is" ,USER_SCORE, "/10")
else:
print(name1," your score is " ,USER_SCORE, "/10")
def no1():
with open("no1.txt", 'a')as file:
file.write(str(name1)+" achieved a score of: "+str(USER_SCORE)+"/10 \n")
def no2():
with open("no2.txt", 'a')as file:
file.write(str(name1)+" achieved a score of "+str(USER_SCORE)+"/10 \n")
def no3():
with open("no3.txt", 'a')as file:
file.write(str(name1)+" achieved a score of "+str(USER_SCORE)+"/10 \n")
if classno=="1":
no1()
if classno=="2":
no2()
if classno=="3":
no3()
答案 0 :(得分:2)
正如我在评论中所说,为了编码,您需要能够将问题分解为更小的组件。每个较小的问题都应该是它们自己的功能。它可以更容易地跟踪事物并解决较小的问题。
以下是您应该进行的各种功能的一些示例。我不能强调你对S.O.的问题。应该关注那些个别问题,而不是一般都不知道如何做事。
尝试填写此结构的空白。
import random
def get_score():
# here do your code to calculate the score
score = random.randint(0,10)
return score
def write_list_to_file():
# for each item in list, write that to a file
pass
def sort_list_alphabetically(unsorted_list):
# figure out how to sort a list one way
return sorted_list
def sort_list_numerically(unsorted_list):
# figure out how to sort a list the other way
return sorted_list
def get_sort_method_from_user():
# get input however you want
if soandso:
return "Alphabetical"
else:
return "Numerical"
def get_user_name():
# do your stuff
return name;
questions = 0
list_of_scores = []
while questions < 10:
name = get_user_name();
user_score = get_score();
output_line = name + " got a score of " + user_score
list_of_scores.append(output_line)
sort_method = get_sort_method_from_user();
if sort_method == "Alphabetical":
new_list = sort_list_alphabetically(list_of_scores)
else:
new_list = sort_list_numerically(list_of_scores)
write_list_to_file(list_of_scores)
答案 1 :(得分:2)
@crclayton
我发现这是按字母顺序排序的,但是,我仍然不知道如何从最高到最低排序文件
viewclass = input(&#34;选择一个班级编号,按字母顺序,平均或最高?&#34;)
if viewclass=='1 alphabetically':
with open('class1.txt', 'r') as r:
for line in sorted(r):
print(line, end='')
elif viewclass=='2 alphabetically':
with open('class2.txt', 'r') as r:
for line in sorted(r):
print(line, end='')
elif viewclass=='3 alphabetically':
with open('class3.txt', 'r') as r:
for line in sorted(r):
print(line, end='')