1

I am trying to get this output:

+-+-+-+-+-+-+
|P|Y|T|H|O|N|
+-+-+-+-+-+-+

Is it possible to get that output without adding new lines in the code?

def art(name):
  for i in range(len(name[0]), len(name) + 1):
    head = "+" + i * "-+"
    middle = "|" + i * "{}|".format(name[0].upper())
    bottom = "+" + i * "-+"
  print(head + "\n" + middle + "\n" + bottom)

art("Python")

Actual output:

+-+-+-+-+-+-+
|P|P|P|P|P|P|
+-+-+-+-+-+-+

I can't figure out how to manipulate it in order to get it to work. I've tried to use use index() and i as a variable but I get errors such as: "IndexError: string index out of range"

2
  • 1
    Why are you providing your actual and target output as screenshots instead of text? (edited: fixed; see meta.stackoverflow.com/a/285557/14122 describing why we frown on screenshots of text) Commented Nov 14, 2021 at 15:00
  • 1
    Put a print(repr(i)) into the loop or, even more useful, learn to use a debugger to step through the code. For the latter, try to find video tutorials for your IDE. As a new user here, please also take the tour and read How to Ask. Commented Nov 14, 2021 at 15:07

2 Answers 2

1

You have hardcoded the index 0 when you're looking up the character to print:

def art(name):
  for i in range(len(name[0]), len(name) + 1):
    head = "+" + i * "-+"
    middle = "|" + i * "{}|".format(name[0].upper())
                                         ^-- here
    bottom = "+" + i * "-+"
  print(head + "\n" + middle + "\n" + bottom)

art("Python")

Instead, you can use i to reference the actual character in name:

middle = "|" + i * "{}|".format(name[i].upper())

You also have an error in your range. There is no need to use len(name[0]) - this will always be 1, and will cause your loop to run one iteration longer than the length of name (since indexes start on 0). This is the cause of your IndexError.

Instead, use 0 as the start - which is the same as not giving a start argument at all:

for i in range(len(name)):

But you don't actually need the index; there is no need to iterate over the name to do what you want. You can instead join each letter in name with | as the separator. The header/footer can be created by repeating +- the same amount of time as the length of name. So your loop can be replaced with:

print('+-' * len(name) + '+') 
print('|' + '|'.join(name.upper()) + '|')
print('+-' * len(name) + '+') 

.. or assigned to head, middle, bottom if you want that instead.

Sign up to request clarification or add additional context in comments.

Comments

0

head and bottom don't need to be in the for loop, because they calculate the full string all at once. For that matter, because they're the same string, there's no reason for them to be two separate variables at all.

Your loop doesn't need to loop over indexes -- it can loop over characters, so you can directly add those characers yourself; and using an f-string is somewhat unnecessarily overcomplicated.

Anyhow -- if you're going to be using a loop, you should be additively constructing your string; just overwriting the whole thing on every iteration makes there no point to using a loop at all.

def art(name):
  head = "+" + len(name) * "-+"
  middle = ''
  for character in name:
    middle += '|{}'.format(character.upper())
  print(head + "\n" + middle + "|\n" + head)

art("Python")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.