2

I'm writing a program for CS class and need help with some python 3 coding. This is my current code written:

def main():
    print() # blank line
    phrase = input("Please enter a phrase: ")
    wordlist = phrase.split()
    print("Original text:",phrase)
    for msg in wordlist:
        print(msg)

output:

    Phil
    likes
    to
    code

I can't use any imports or things like that. I can only use small things like loops or slices or split. Any help would be appreciated. I need the output to look like:

P l t c
h i o o
i k   d
l e   e
  s
Cœur
  • 37,241
  • 25
  • 195
  • 267
CSstudent
  • 41
  • 1
  • 4
  • so you got the first step you need to do ..... what do you think the next step should be ... I dont actually see any question here... I guess a hint would be you cant actually print things vertically(well you can but that starts getting very complicated using curses or something) ... only horizontally – Joran Beasley Sep 29 '15 at 03:58

2 Answers2

1

You can use itertools.zip_longest() with fillvalue as space. Example -

>>> s = "Four score and seven years ago"
>>> ls = s.split()
>>> import itertools
>>> for i in itertools.zip_longest(*ls,fillvalue=' '):
...     print(*i)
...
F s a s y a
o c n e e g
u o d v a o
r r   e r
  e   n s

itertools.izip_longest for Python 2 .

Sharon Dwilif K
  • 1,218
  • 9
  • 17
0

Without imports, as you asked:

words = phrase.split()
height = max(map(len, words))
padded = [word.ljust(height) for word in words]
for row in zip(*padded):
    print(' '.join(row))

I hope that's ok for you.

Veky
  • 2,646
  • 1
  • 21
  • 30
  • 1
    I wasn't able to use all the code you gave me but I was able to get an idea of it and make my code work correctly. Thanks for your help! – CSstudent Sep 29 '15 at 14:33
  • May I just ask why you weren't able to use the code above? (Moral or technical reasons?:) – Veky Sep 30 '15 at 09:45
  • Moral reasons. Couldn't use everything there because professor didn't show us yet – CSstudent Sep 30 '15 at 17:19