【问题标题】:How to use tabulate module with user input and loops in Python?如何在 Python 中使用带有用户输入和循环的制表模块?
【发布时间】:2021-09-14 11:02:56
【问题描述】:

我做了一个程序,用户输入学生的分数,Python 告诉平均分,告诉我们分数是令人满意的,好还是优秀。如下:

from array import array as arr
import numpy as np
from tabulate import *

marks = []
stu_mks = input("Enter the marks of the students(entering STOP would end the process): ")
marks.append(stu_mks)

while stu_mks.lower() != 'stop':
     stu_mks = input("Enter the marks of the students(entering STOP would end the process): ")
     marks.append(stu_mks)

del marks[-1]

array_marks = np.array(marks)
float_marks = array_marks.astype(np.float)

avg = 0
for i in float_marks:
    avg += i

print("The average marks are", avg / len(float_marks))

for x in float_marks:
    if x % 1 == 0:
        print(int(x), end = "\t")
        if x < 40:
            print("NOT SATISFACTORY")
        if x > 40 and x <= 74:
            print("SATISFACTORY")
        if x >= 75 and x <= 89:
            print("GOOD")
        if x >= 90:
            print("EXCELLENT")
    else:
        print(x, end = "\t")
        if x < 40:
            print("NOT SATISFACTORY")
        if x > 40 and x <= 74:
            print("SATISFACTORY")
        if x >= 75 and x <= 89:
            print("GOOD")
        if x >= 90:
            print("EXCELLENT")

我真正想要的是分数和他们的评论(即好、优秀等)以表格格式显示。这就是我导入tabulate 模块的原因,但问题是我不知道如何将每一行作为列表或字典添加到任何变量中,然后将其制成表格。我非常努力,但一切都是徒劳的。所以,请给我建议一种方法来做同样的事情,如果有更好的方法以更少的代码行运行这个程序,那将是非常可观的。提前谢谢你。

【问题讨论】:

    标签: python tabulate


    【解决方案1】:

    好吧,我已经解决了!这很容易。代码如下:

    from array import array as arr
    import numpy as np
    from tabulate import *
    
    marks = []
    stu_mks = input("Enter the marks of the students(entering STOP would end the process): ")
    marks.append(stu_mks)
    
    while stu_mks.lower() != 'stop':
         stu_mks = input("Enter the marks of the students(entering STOP would end the process): ")
         marks.append(stu_mks)
    
    del marks[-1]
    
    array_marks = np.array(marks)
    float_marks = array_marks.astype(np.float)
    
    avg = 0
    for i in float_marks:
        avg += i
    
    print("The average marks are", avg / len(float_marks))
    
    marks_list = []
    remarks_list = []
    
    for x in float_marks:
        if x % 1 == 0:
            marks_list.append(int(x))
            if x < 40:
                remarks_list.append("NOT SATISFACTORY")
            if x > 40 and x <= 74:
                remarks_list.append("SATISFACTORY")
            if x >= 75 and x <= 89:
                remarks_list.append("GOOD")
            if x >= 90:
                remarks_list.append("EXCELLENT")
        else:
            marks_list.append(x)
            if x < 40:
                remarks_list.append("NOT SATISFACTORY")
            if x > 40 and x <= 74:
                remarks_list.append("SATISFACTORY")
            if x >= 75 and x <= 89:
                remarks_list.append("GOOD")
            if x >= 90:
                remarks_list.append("EXCELLENT")
    
    table = {'Marks Scored' : marks_list, 'Remarks' : remarks_list}
    
    print(tabulate(table, headers='keys', tablefmt='fancy_grid', stralign = 'center', numalign = 'center'))
    

    我制作了一个名为 table 的字典,并制作了两个名为 marks_listremarks_list 的空列表。在marks_list 中,我按照用户输入的顺序附加了所有标记,并且以同样的方式,所有与标记对应的备注都附加到了remarks_list。 我只编辑了以下代码块:

    marks_list = []
    remarks_list = []
    
    for x in float_marks:
        if x % 1 == 0:
            marks_list.append(int(x))
            if x < 40:
                remarks_list.append("NOT SATISFACTORY")
            if x > 40 and x <= 74:
                remarks_list.append("SATISFACTORY")
            if x >= 75 and x <= 89:
                remarks_list.append("GOOD")
            if x >= 90:
                remarks_list.append("EXCELLENT")
        else:
            marks_list.append(x)
            if x < 40:
                remarks_list.append("NOT SATISFACTORY")
            if x > 40 and x <= 74:
                remarks_list.append("SATISFACTORY")
            if x >= 75 and x <= 89:
                remarks_list.append("GOOD")
            if x >= 90:
                remarks_list.append("EXCELLENT")
    
    table = {'Marks Scored' : marks_list, 'Remarks' : remarks_list}
    
    print(tabulate(table, headers='keys', tablefmt='fancy_grid', stralign = 'center', numalign = 'center'))
    

    然后我按照制作表格的一般方法,您可以在此处找到:

    链接1: https://towardsdatascience.com/how-to-easily-create-tables-in-python-2eaea447d8fd

    链接 2: https://pypi.org/project/tabulate/

    宾果游戏!!!

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • @Community♦ 我已经编辑了我的答案。所以,请检查一下现在没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多