在Python中以漂亮的格式显示数据,用于小学课堂项目



我正在做一个项目,用于一所小学,教师和学生将提交一系列形容词和名词,然后我们将在每周的创意乐高拼搭挑战中使用。

我正在尝试学习python,所以请耐心等待。我创建了一个程序,要求人们提交26个形容词和10个名词。我想截取屏幕截图并用作孩子们构建的指南。

我不确定如何创建一个表格或其他结构来在一个屏幕上显示所有信息并且更具可读性。

我希望得到任何帮助!我的代码如下。

print("Thank you for helping with a creative LEGO project")
print("Please remember to keep words school safe and readable for elementary students")

adj1 = input("Give me a adjective ")
adj2 = input("Give me a adjective ")
adj3 = input("Give me a adjective ")
adj4 = input("Give me a adjective ")
adj5 = input("Give me a adjective ")
adj6 = input("Give me a adjective ")
adj7 = input("Give me a adjective ")
adj8 = input("Give me a adjective ")
adj9 = input("Give me a adjective ")
adj10 = input("Give me a adjective ")
adj11 = input("Give me a adjective ")
adj12 = input("Give me a adjective ")
adj13 = input("Give me a adjective ")
adj14 = input("Give me a adjective ")
adj15 = input("Give me a adjective ")
adj16 = input("Give me a adjective ")
adj17 = input("Give me a adjective ")
adj18 = input("Give me a adjective ")
adj19 = input("Give me a adjective ")
adj20 = input("Give me a adjective ")
adj21 = input("Give me a adjective ")
adj22 = input("Give me a adjective ")
adj23 = input("Give me a adjective ")
adj24 = input("Give me a adjective ")
adj25 = input("Give me a adjective ")
adj26 = input("Give me a adjective ")
print("Nice job coming up with all of those adjectives")
print("Now we are moving on to nouns")
print("Here we go!")
noun1 = input("Give me a noun ")
noun2 = input("Give me a noun ")
noun3 = input("Give me a noun ")
noun4 = input("Give me a noun ")
noun5 = input("Give me a noun ")
noun6 = input("Give me a noun ")
noun7 = input("Give me a noun ")
noun8 = input("Give me a noun ")
noun9 = input("Give me a noun ")
noun10 = input("Give me a noun ")

print("A = " + adj1)
print("B = " + adj2)
print("C = " + adj3)
print("D = " + adj4)
print("E = " + adj5)
print("F = " + adj6)
print("G = " + adj7)
print("H = " + adj8)
print("I = " + adj9)
print("J = " + adj10)
print("K = " + adj11)
print("L = " + adj12)
print("M = " + adj13)
print("N = " + adj14)
print("O = " + adj15)
print("P = " + adj16)
print("Q = " + adj17)
print("R = " + adj18)
print("S = " + adj19)
print("T = " + adj20)
print("U = " + adj21)
print("V = " + adj22)
print("W = " + adj23)
print("X = " + adj24)
print("Y = " + adj25)
print("Z = " + adj26)
print("1-3 = " + noun1)
print("4-6 = " + noun2)
print("7-9 = " + noun3)
print("10-12 = " + noun4)
print("13-15 = " + noun5)
print("16-18 = " + noun6)
print("19-21 = " + noun7)
print("22-25 = " + noun8)
print("26-28 = " + noun9)
print("29-31 = " + noun10)

首先,您可以合成您的代码(实际上,您可以使用意图列表在下面更浓缩

(:
adjectives_list = []
for ka in range(0,26):
print(" Give me an adjective ")
adjectives_list.append(input())
nouns_list = []
for kn in range(0,10):
print(" Give me a noun ")
nouns_list.append(input())

在不太清楚你想为我做什么之后。如果可能有一些数据操作,熊猫库可能是一个不错的选择。如果只是关于打印,这是一个味道问题,但我会编写一个函数来完成这项工作,如果有必要,可能会从一个代表你正在处理的问题的类中。

这是一种更简洁(和通用(的方式来做你已经在做的事情。

print("Thank you for helping with a creative LEGO project")
print("Please remember to keep words school safe and readable for elementary students")
num_adjectives = 26
adjectives = [input() for _ in range(num_adjectives)]
print("Nice job coming up with all of those adjectives")
print("Now we are moving on to nouns")
print("Here we go!")
num_nouns = 10
nouns = [input() for _ in range(num_nouns)]
alphabet = 'ABCEFGHIJKLMNOPQRSTUVWXYZ'
for idx,letter in enumerate(alphabet):
print(f'{letter} = {adjectives[idx]}')
for idx,noun in enumerate(nouns):
print(f'{idx*3+1}-{idx*3+3} = {noun}')

在我的建议之前,我想指出,为此任务编写一个 Python 程序可能不是一个理想的解决方案。

鉴于您如何描述您的目标,在我看来,Google Forms或其他调查服务更容易设置,也更容易人们填写。使用电子表格中的结果,您可能会发现更容易操作它们以显示您想要的样子。


其他人提出了如何简化程序的建议,因此有一些关于如何压缩输出的简单想法。

我做了两个假设:正如其他答案中所建议的那样,您将用户的输入放在名为nounsadjectives的列表中,并且您正在将输出打印到终端。

首先,您可能希望使用str.join将单词列表格式化为逗号分隔的单词。然后,您可以使用textwrap.fill在单词之间而不是单词中间分隔行

import textwrap, shutil
# Use the other answers' suggestions to put words into these lists.
nouns = []
# Some basic pre-processing using <generator expression> (see also <list comprehensions>)
# Remove trailing spaces, make all-lowercase, remove duplicates, and sort alphabetically
nouns = sorted(set(noun.strip().lower() for noun in nouns))
# Get width of terminal (80 if not using a terminal)
width = shutil.get_terminal_size().columns
print("Nouns",
"=====",
textwrap.fill(", ".join(nouns), width),
sep="n")  # separate outputs by newlines

要执行更高级的格式设置,例如将单词格式化为表格,您需要更多地处理数据,因为您需要知道每列的宽度。或者,您可以使用现有库之一。例如,NumPy是一个流行的科学软件包,其最大功能是高级(多维(数组,它将它们整齐地格式化为网格以供显示。一个更高级,但可能更有用的库是Pandas,它主要围绕一个对象(DataFrame(,该对象或多或少地表示数据表,包括标题。


此外,如果你向小学生索要单词,即使您告诉他们要保证这些单词在学校的安全,也一定要自己检查一下,因为认为不恰当的单词很有趣的孩子不太可能听(有些人甚至可能会因为你要求他们不要听(。您还需要查看单词以删除重复项并更正拼写错误。

最新更新