2

I'm trying to write a code to have a phrase print vertically and it needs to be printed a certain away. I'm having the user input a string but I need the output to look a certain way with spaces. I have some of the code written already:

def main():

    #have user input phrase
    phrase = input("Enter a phrase:  ")
    print() # turnin
    print() #blank line
    print("Original phrase:",phrase)
    print() # blank line

    word_l = phrase.split()
    word_a = len(word_l)

    max_len = 6
    for i in range(word_a):
        length = len(word_l[i])
        if length < max_len:
             print(len(word_l[i]) * " ",end="")

I need to have 2 loops inside each other for the next part but I don't believe the above loop and if statement are correct. So the say the user inputs the phrase: Phil likes to code. I need the output to look like:

P    l     t  c
h    i     o  o
i    i        d
l    k        e
     e
     s

The spaces in between the words are the spaces as if the letters were there including one space. I can't use any imports and the only function I can use is split. I need to have a for loop with an if statement there and then I need another for loop with a for loop inside of that. Would really appreciate any help.

CSstudent
  • 41
  • 1
  • 4
  • What output does your current code give you? Please fix your indentation, or is that what the code looks like? – Totem Sep 30 '15 at 17:31
  • you already broke the rules ... you are using `len` and you are using `range` and you are using `input` and `print` ... so are you only allowed to use `split,len,range,input,print `? – Joran Beasley Sep 30 '15 at 17:39
  • might I make a suggestion? In the interest of you getting your code working faster, and everyone having an easier time to test, why dont you comment out the phrase prompting and just have **phrase = "Phil likes to code"**. It will improve your testing cycle speed immensely and once that works you can re-enable prompting. – JL Peyret Sep 30 '15 at 18:36

2 Answers2

6

An easier method is to use zip_longest from itertools

import itertools

txt = "some random text"

l = txt.split(' ')

for i in itertools.zip_longest(*l, fillvalue=" "):
    if any(j != " " for j in i):
        print(" ".join(i))

The previous code will give you

s r t
o a e
m n x
e d t
  o  
  m  

To add additional spaces between the words increase spaces in print(" ".join(i))

You can change txt to input of course

Leb
  • 15,483
  • 10
  • 56
  • 75
  • @JoranBeasley I see it now, *I can't use any imports*. Well, don't print the `import` lol – Leb Sep 30 '15 at 17:42
  • and the only function he can use is `split` apparently – Joran Beasley Sep 30 '15 at 17:44
  • 1
    I think that's absolutely useless, it reminds me when I had to take a probability theory exam without a calculator. All those fractions... If a tool is available use it, it's there for a reason not to take space on your hdd. – Leb Sep 30 '15 at 17:48
3

How about this?

phrase = "Phil likes to code"

words = phrase.split()

maxlen = max([len(w) for w in words])


#now, you dont need max len , you can do a while True
#and break if all the words[ix] hit an IndexError

print "012346789"
for ix in range(maxlen):
    line = ""
    for w in words:

        try:
            line += w[ix]
        except IndexError:
            line += " "
        line += " "
    print line

and the output is:

012346789
P l t c
h i o o
i k   d
l e   e
  s
JL Peyret
  • 10,917
  • 2
  • 54
  • 73
  • oh, and sorry, print x becomes print(x) in Python 3. – JL Peyret Sep 30 '15 at 18:54
  • last, if I understood your reqs correctly, you'd have to keep track of your separators between words. i.e. instead of line += " " at the end, you'd have to do something like line += separators[w_counter]. separators[] would have 1 or more spaces depending on what was in the input phrase – JL Peyret Sep 30 '15 at 19:04