Need help with ASCII type box in Python - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need help with ASCII type box in Python

HauntJemimahHauntJemimah United StatesPosts: 0Member

Hello,

I'm trying to make an ASCII code type box (the ones you see on GameFAQs of famous logos and such) since its part of the Chapter 6 Lab for Programming Arcade Games with Pygame.

Here is what I have so far. The trouble I'm having is to determine how to space out the middle line. I know this depends on what n equals and I've been trying to tweak it to match any number of 'n'.

The line I am having trouble with is Line 39

I'm trying to calculate how many spaces I actually need by multiplying it by n*2 to get the max number across plus a little bit for one extra space.

This only works for n = 4, so I think I messed up pretty badly sadly enough.

I do this because you can't just add a number to a space character and I believe you can only multiply or divide a space by a number.

I was even thinking about rethinking this and converting whatever I obtain into int using int(), but I might be overthinking things.

Here's my code:

    # n = row number

    # Try 3 rows:

    n = 4

    for i in range(n):
        for j in range(n*2):
            # Add if statements to limit the cases to draw a line of o's
            # on the top and bottom lines for the box but spaces
            # with an o on each side for all the lines in between:

            # Top row:
            if i == 0:
                print("o", end = " ")
            # Middle cases (maintain first row to last row, but limit to 1st column)
            if i > 0 and i < n-1 and j < 1:
                print("o", end = " ")
            # Maintain first row to last row, but limit to last column based on n*2:
            if i > 0 and i < n-1 and j == (n): # 13 spaces needed for n = 4
                print(" " * ((n*2)+(n-1)), "o", end = " ")
            # Bottom row:
            if i == n-1:
                print("o", end = " ")
        print()

    # Fix the middle case

# Chapter 6 Lab Link:
# http://programarcadegames.com/index.php?chapter=lab_loopy_lab

This is my output so far (ignore the first case since it was in an unrelated part of the lab

*** Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32. ***
*** Remote Python engine  is active ***
>>> 
*** Remote Interpreter Reinitialized  ***
>>> 
10 
11 12 
13 14 15 
16 17 18 19 
20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 37 
38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 
o o o o o o o o 
o             o 
o             o 
o o o o o o o o 
>>>

Comments

  • slook26slook26 UtahPosts: 4Member
    edited January 27

    Depending on the text in the terminal/CLI the space usually is the same, in Python 2 you can just multiply the characters out to make length e.g.: def print_squarebox_lines(n,l): print "x" * n for i in range(l): print "x" + " " * (n-2) + "x" print "x" * n `` print_squarebox_lines(3,3): xxx x x x x x x xxx

    Not sure how python 3 stacks up

Sign In or Register to comment.