【问题标题】:How can I print the random list of grades without copying the same grade of the other students?如何在不复制其他学生相同成绩的情况下打印随机成绩列表?
【发布时间】:2020-05-21 06:10:02
【问题描述】:

我不知道如何使用随机包打印学生的成绩而不复制其他学生的相同成绩。该程序需要功能来制作。请帮忙

import random


def get_values():
    list = []
    q = 1
    i = 4
    for num in range(i):
        data = random.randint(1, 100)
        list.append(data)
        #print("Quiz no.", q)
        #print(data)
        q += 1

    return list


def validate(ctr):
    while ctr > 50:
        print("Maximum of 50 students only")
        exit()
    return ctr


def grade(x):
    FG = sum(x) / 4
    return FG


def EQ(FG):
    if FG < 60:
        s = float(5.0)
        print(s)

    elif FG >= 60 and FG < 75:
        s = float(3.0)
        print(s)

    elif FG >= 75 and FG < 90:
        s = float(2.0)
        print(s)

    elif FG >= 90 and FG <= 100:
        s = float(1.0)
        print(s)

    return s


def main():
    list_high = []
    list_low = []
    ls=[]
    repeat = 'Y'
    ctr = 1
    while repeat.upper() == 'Y':
        validate(ctr)
        x = get_values()
        y = grade(x)
        z = EQ(y)
        high = list_high.append(max(x))
        low = list_low.append(min(x))

        print("Student #  Quiz 1     Quiz2    Quiz 3    Quiz4    Grade      EQ")
        q = 1

        for num in range(ctr - 1):
            print("\n   ", q, end='')

            for c in range(len(x)):
                print("       ", x[c], end="")
            q += 1
            print("    ", y, "    ", z, end=" ")

        ctr += 1

       # average_high = sum(list_high) / len(list_high)
        #average_low = sum(list_low) / len(list_low)


        repeat = input("\n Enter Y to input another students score: ")



main()

我的程序的输出

学生 # 测验 1 测验 2 测验 3 测验 4 年级 EQ

1        17        95        5        92     52.25      5.0 
2        17        95        5        92     52.25      5.0 
3        17        95        5        92     52.25      5.0 
4        17        95        5        92     52.25      5.0 
5        17        95        5        92     52.25      5.0 
6        17        95        5        92     52.25      5.0 

输入 Y 输入另一个学生的分数:

【问题讨论】:

    标签: python


    【解决方案1】:

    你想多了。此外,如果可以,请尝试迭代对象集合而不是基于范围的 for 循环。

    这里可能会有所帮助:

    def main():
        from random import sample
    
        number_of_quizzes = 4
    
        scores = sample(range(1, 100), number_of_quizzes)
        print(scores)
    
    if __name__ == "__main__":
        main()
    

    输出:

    [10, 93, 66, 46]
    >>> 
    

    然后,例如,如果你有五个学生,每个学生有四个测验,你可以像这样扩展它:

    def main():
        from random import sample
    
        number_of_students = 5
        number_of_quizzes = 4
    
        for current_scores in (sample(range(1, 100), number_of_quizzes) for _ in range(number_of_students)):
            print(current_scores)
    
    if __name__ == "__main__":
        main()
    

    输出:

    [25, 82, 87, 13]
    [91, 12, 76, 65]
    [96, 90, 19, 34]
    [82, 67, 4, 8]
    [6, 91, 47, 68]
    >>> 
    

    编辑 - 这更接近你所写的:

     def get_random_scores():
        from random import randint
    
        number_of_scores = 4
        scores = []
    
        for _ in range(number_of_scores):
            score = randint(1, 100)
            scores.append(score)
        return scores
    
    def get_highest_score_for_quiz_n(students_scores, *, n):
        return max(list(zip(*students_scores))[n-1])
    
    
    def main():
    
        number_of_students = 5
        students_scores = []
        for _ in range(number_of_students):
            students_scores.append(get_random_scores())
    
        print("The scores are:")
        for scores in students_scores:
            print(scores)
    
        max_third_quiz_score = get_highest_score_for_quiz_n(students_scores, n=3)
        print(f"The highest score on the third quiz was {max_third_quiz_score}")
    
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

    • 还有其他方法吗?喜欢使用基本算法,因为我们的老师要求我们只使用基本代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2021-12-28
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多