0

I am working on a project to use in an elementary school where teachers and students will submit a list of adjectives and nouns that we will then use in a creative LEGO build challenge each week.

I am trying to learn python so please bear with me. I created a program asking people to submit 26 adjectives and 10 nouns. I want to take the screenshot and use as a the guide for the kids to build with.

I am not sure how to create a table or other structure to display the information all on one screen and more readable.

I would love any help! My code is below.

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)
Aaron Maurer
  • 21
  • 1
  • 4
  • 3
    Time to learn about [lists](https://docs.python.org/2/tutorial/datastructures.html) and `for` loops! – ForceBru Feb 14 '20 at 19:57
  • Thank you. I will search these out. I was not sure about what to even learn next. Appreciate the guidance – Aaron Maurer Feb 14 '20 at 20:00
  • 2
    Speaking about printing tables, there are [some](https://pypi.org/project/tabulate/) [libraries](https://github.com/jazzband/prettytable) for that. Also, possible duplicate: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – ForceBru Feb 14 '20 at 20:05
  • 1
    If you consider collecting and displaying the data more important than learning how to write the program, using an online survey program like Google Forms might probably be easier. That would also give you results in a relatively easy-to-use useful format (e.g. Google Sheets or Excel), which you could use to format the results as you see fit. – jirassimok Feb 14 '20 at 20:16

3 Answers3

1

First you can synthetize your code (in fact you can ever more condensate that below with intentions lists) :

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())

After it's not so clear what you're trying to do for me. If it's possible you have some data manipulation, pandas library can be a good option. If it's just about printing, it's a matter of tast, but I would a write a function doing the job, maybe from a class representing the problem you're working on, if necessary.

Guilhem L.
  • 421
  • 2
  • 8
0

This is a much more succinct (and general) way to do what you're already doing.

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}')
Mark Snyder
  • 1,635
  • 3
  • 12
  • As the asker appears to be new to Python, I suggest adding some comments explaining how parts of that work (list comprehension, format strings, `enumerate`). If you want to make this into an actual answer, maybe use `str.join` or something to print the lists a bit more nicely. – jirassimok Feb 14 '20 at 20:42
0

Before my suggestions, I'd like to note that writing a Python program for this task may not be an ideal solution.

Given how you described your goal, it sounds to me like Google Forms or another survey service would easier to set up and easier for people to fill out. And with the results in a spreadsheet, you might find it easier to manipulate them to look how you want to.


Others have made suggestions about how to simplify your program, so are some simple ideas for how to condense the output..

I make two assumptions: that you have the user's inputs in lists called nouns and adjectives, as suggested in the other answers, and that you are printing your output to a terminal.

First, you might want to format the word lists as comma-separated words using str.join. Then, you can use textwrap.fill to split lines between words instead of in the middle of words.

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

To do more advanced formatting, like formatting the words as a table, you need to process the data a bit more, because you need to know how wide each column can be. Alternatively, you can use one of the existing libraries. For example, NumPy is a popular scientific package with advanced (multi-dimensional) arrays as its biggest feature, and it neatly formats them in a grid for display. A more advanced, but possibly more useful library is Pandas, which largely revolves around an object (the DataFrame) that more or less represents a table of data, including headers.


Also, if you are asking for words from schoolchildren, even if you tell them to keep the words safe for school, make sure to look them over yourself, because children who think inappropriate words are funny are not likely to listen (some might even put them in just because you asked them not to). You'll also want to look over the words to remove duplicates and correct misspellings.

jirassimok
  • 3,850
  • 2
  • 14
  • 23