Python输出根据用户输入遍历列表

时间:2018-11-21 16:50:32

标签: python

我一直在努力处理此代码达两个小时之久,并且在以前与之相关的问题中看不到太多。我想尽办法使它正常工作,所以我认为它在这一点上效率很低,而且没有以我想要的方式输出。

我试图以多个成绩条目的形式获取用户输入,根据评分标准对其进行评估,然后以字母形式输出学生的成绩

任何有关使其正确输出或采用任何更好的编码技术的建议,将不胜感激。提前感谢新手编码员。

这是我的代码:

    """10.1 - write a program that reads a list of scores and then
    assigns letter grades based on the criteria:
    A if score is >= best - 10
    B if score is >= best - 20
    C if score is >= best - 30
    D if score is >= best - 40
    F otherwise
    """

    grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
    grades_list = grades_input.split() #split user input into a list
    grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
    number_of_students = []

    for i in range(len(grades_list_valid)):
        number_of_students.append(i)

    for grade in grades_list_valid: #create criteria to assign each letter grade
        best_score = max(grades_list_valid) #get the highest grade


        if grade >= best_score - 10:
            print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
        elif grade >= best_score - 20:
            print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
        elif grade >= best_score - 30:
            print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
        elif grade >= best_score - 40:
            print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
        else:
            print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")

这是输出:

    Enter Students scores seperated by a space: 40 55 70 58
    Student 3 score is 58 and grade is C
    Student 3 score is 58 and grade is B
    Student 3 score is 58 and grade is A
    Student 3 score is 58 and grade is B

所需的输出是:

    Student 0 score is 40 and grade is C
    Student 1 score is 55 and grade is B
    Student 2 score is 70 and grade is A
    Student 3 score is 58 and grade is B

3 个答案:

答案 0 :(得分:0)

这是因为在使用i之前,完成获取i的第一个循环已完成,因此当您遍历打印结果时,i已经是3。相反,您应该像这样在第二个循环中获取索引:

grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers

best_score = max(grades_list_valid) 
for i, grade in enumerate(grades_list_valid): 
    letter_grade = "F"        

    if grade >= best_score - 10:
        letter_grade = "A"
    elif grade >= best_score - 20:
        letter_grade = "B"
    elif grade >= best_score - 30:
        letter_grade = "C"
    elif grade >= best_score - 40:
        letter_grade = "D"
    else:
        letter_grade = "F"

    print("Student {} score is {} and grade is {}".format(i, grade, letter_grade))

,然后消除第一个循环。

再次查看问题后,我还注意到您每次都从同一列表中获取“最佳分数”,这会使每个学生获得相同的分数。我想你只是想为每个学生拿分数。我更新了答案以解决该问题-不确定这是否是您想要的行为。

编辑:再次查看它,意识到您在做什么时获得了最高分,修复了我的答案,这是我运行它时的结果:

Enter Students scores seperated by a space: "70 80 93 77 31"
Student 0 score is 70 and grade is C
Student 1 score is 80 and grade is B
Student 2 score is 93 and grade is A
Student 3 score is 77 and grade is B
Student 4 score is 31 and grade is F

答案 1 :(得分:0)

grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
number_of_students = []

for i in range(len(grades_list_valid)):
    number_of_students.append(i)
i=0
for grade in grades_list_valid: #create criteria to assign each letter grade
    best_score = max(grades_list_valid) #get the highest grade

    print(i)
    if grade >= best_score - 10:
        print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
    elif grade >= best_score - 20:
        print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
    elif grade >= best_score - 30:
        print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
    elif grade >= best_score - 40:
        print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
    else:
        print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")
    i+=1

这是正确的方法,在您的代码i = 3中,这就是为什么每个学生得到相同答案的原因

答案 2 :(得分:0)

正如Quinn所说,您的顶部循环遍历i,结束于i = 3。以后对i的引用将产生3。 我注意到了一些事情:

1)您可以做的是在遍历的同时检查其成绩。

2)此外,您无需查找每个循环的最大值,只需在循环 之外获取一次,然后在内部进行引用即可。

3)最后,当您拥有所有这些打印语句时,它看起来很丑陋,如果您在if语句中指定成绩,然后使用找到的成绩打印一次,则读取起来会更好,就像这样: >

best_score = max(grade_list_valid)
for i in range(len(grades_list_valid)):
    curr_score = grades_list_valid[i]
    if(curr_score >= best_score - 10):
        grade = "A"
    ...
    print("Student {} score is {} and grade is {}".format(i, curr_score, grade))

您还可以进行一些其他小的优化。例如,您具有以下列表理解:

[ int(x) for x in grades_list ]

map的经典案例:

map(int, grades_list)