Jump to content

Python 3 Weird print() Function Output

Go to solution Solved by elpiop,

I believe it is because you are printing your draw DrawToScreen function. Since DrawToScreen does not return anything it ends up printing None. Which I guess is what you said, so yeah you are correct. 

To start this off: I was given a lab assignment to draw "ASCII art". The task consisted of outputting my initials, a house, and an animal. I completed the task, it has already been turned in, and graded. Neither my teacher, TA, nor myself can find a solution to the problem that is outlined below.

The goal of the program is to draw "ASCII art" to the console. I chose to use lists of lists to represent a 2 dimensional plane instead of writing a bunch of print() statements because later in the year the teacher/TA is going to break the program and make us fix it. I believe that this will be much easier to "maintain" later. Here is the code in question (sorry for no highlighting, none of the options works well with python)

 

# Initials returns a 2 dimensional array (list of lists) with the mapping for my initials. Rows = 7, Columns = 12
def Initials():

    myInitials = [[], [], [], [], [], [], [], [], [], [], [], []]

    # Initialize all sub arrays.
    for i in range(0, 7):
        for j in range(0, 12):
            myInitials[i].append(j)

    # Initialize first row.
    myInitials[0][0] = 32
    myInitials[0][1] = 95
    myInitials[0][2] = 95
    myInitials[0][3] = 95
    myInitials[0][4] = 95
    myInitials[0][5] = 95
    myInitials[0][6] = 32
    myInitials[0][7] = 32
    myInitials[0][8] = 95
    myInitials[0][9] = 95
    myInitials[0][10] = 95
    myInitials[0][11] = 32

    # Initialize second row.
    myInitials[1][0] = 32
    myInitials[1][1] = 32
    myInitials[1][2] = 32
    myInitials[1][3] = 124
    myInitials[1][4] = 32
    myInitials[1][5] = 32
    myInitials[1][6] = 32
    myInitials[1][7] = 32
    myInitials[1][8] = 124
    myInitials[1][9] = 32
    myInitials[1][10] = 124
    myInitials[1][11] = 32

    # Initialize third row.
    myInitials[2][0] = 32
    myInitials[2][1] = 32
    myInitials[2][2] = 32
    myInitials[2][3] = 124
    myInitials[2][4] = 32
    myInitials[2][5] = 32
    myInitials[2][6] = 32
    myInitials[2][7] = 32
    myInitials[2][8] = 124
    myInitials[2][9] = 32
    myInitials[2][10] = 124
    myInitials[2][11] = 32

    # Initialize fourth row.
    myInitials[3][0] = 32
    myInitials[3][1] = 32
    myInitials[3][2] = 32
    myInitials[3][3] = 124
    myInitials[3][4] = 32
    myInitials[3][5] = 32
    myInitials[3][6] = 32
    myInitials[3][7] = 32
    myInitials[3][8] = 124
    myInitials[3][9] = 95
    myInitials[3][10] = 95
    myInitials[3][11] = 95

    # Initialize fifth row.
    myInitials[4][0] = 32
    myInitials[4][1] = 32
    myInitials[4][2] = 32
    myInitials[4][3] = 124
    myInitials[4][4] = 32
    myInitials[4][5] = 32
    myInitials[4][6] = 32
    myInitials[4][7] = 32
    myInitials[4][8] = 124
    myInitials[4][9] = 32
    myInitials[4][10] = 32
    myInitials[4][11] = 124

    # Initialize sixth row.
    myInitials[5][0] = 32
    myInitials[5][1] = 32
    myInitials[5][2] = 32
    myInitials[5][3] = 124
    myInitials[5][4] = 32
    myInitials[5][5] = 32
    myInitials[5][6] = 32
    myInitials[5][7] = 32
    myInitials[5][8] = 124
    myInitials[5][9] = 32
    myInitials[5][10] = 32
    myInitials[5][11] = 124

    # Initialize seventh row.
    myInitials[6][0] = 95
    myInitials[6][1] = 95
    myInitials[6][2] = 95
    myInitials[6][3] = 47
    myInitials[6][4] = 32
    myInitials[6][5] = 32
    myInitials[6][6] = 32
    myInitials[6][7] = 32
    myInitials[6][8] = 124
    myInitials[6][9] = 95
    myInitials[6][10] = 95
    myInitials[6][11] = 124

    # Pass the jagged array back to the caller.
    return myInitials

# DrawToScreen takes a 2 dimensional array containing decimal ASCII chars and draws them to the screen
# as if it were art. All maps must be regular rectangles, with the format being myList[rows][columns]
def DrawToScreen( charMap, rows, columns ):

    for i in range(0, rows):
        for j in range(0, columns):
            print(chr(charMap[i][j]), end="")
        print("")

# testing how to print the picture to the screen. For some reason, writes "None" under all output.
print(DrawToScreen(Initials(), 7, 12))

When run, the final print() statement that calls DrawToScreen exits after writing "None" to the console, after successfully drawing the art as intended.

Come to think of it, I may have just answered my own question. Is the correct answer: By embedding the DrawToScreen function in a print statement, am I causing nested print statements, with the outer print statement having nothing to print, so prints "None"?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Bump

 

I've been out of coding for too long to help, but hopefully this bump helps you.

Cor Caeruleus Reborn v6

Spoiler

CPU: Intel - Core i7-8700K

CPU Cooler: be quiet! - PURE ROCK 
Thermal Compound: Arctic Silver - 5 High-Density Polysynthetic Silver 3.5g Thermal Paste 
Motherboard: ASRock Z370 Extreme4
Memory: G.Skill TridentZ RGB 2x8GB 3200/14
Storage: Samsung - 850 EVO-Series 500GB 2.5" Solid State Drive 
Storage: Samsung - 960 EVO 500GB M.2-2280 Solid State Drive
Storage: Western Digital - Blue 2TB 3.5" 5400RPM Internal Hard Drive
Storage: Western Digital - BLACK SERIES 3TB 3.5" 7200RPM Internal Hard Drive
Video Card: EVGA - 970 SSC ACX (1080 is in RMA)
Case: Fractal Design - Define R5 w/Window (Black) ATX Mid Tower Case
Power Supply: EVGA - SuperNOVA P2 750W with CableMod blue/black Pro Series
Optical Drive: LG - WH16NS40 Blu-Ray/DVD/CD Writer 
Operating System: Microsoft - Windows 10 Pro OEM 64-bit and Linux Mint Serena
Keyboard: Logitech - G910 Orion Spectrum RGB Wired Gaming Keyboard
Mouse: Logitech - G502 Wired Optical Mouse
Headphones: Logitech - G430 7.1 Channel  Headset
Speakers: Logitech - Z506 155W 5.1ch Speakers

 

Link to comment
Share on other sites

Link to post
Share on other sites

I believe it is because you are printing your draw DrawToScreen function. Since DrawToScreen does not return anything it ends up printing None. Which I guess is what you said, so yeah you are correct. 

Link to comment
Share on other sites

Link to post
Share on other sites

I believe so, print will print the return of a function or of a whatever if given, not the actually program. Just call the function, no need to print it. 

 

Quote

def DrawToScreen( charMap, rows, columns ):

    for i in range(0, rows):
        for j in range(0, columns):
            print(chr(charMap[i][j]), end="")
        print("")

# testing how to print the picture to the screen. For some reason, writes "None" under all output.
DrawToScreen(Initials(), 7, 12)

 

 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, elpiop said:

Since DrawToScreen does not return anything it ends up printing None.

Almost. Unlike other languages, every function in Python returns something. If no return statement is hit (or a return without a value) in the function it defaults to returning None.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×