sergio
sergio

Reputation: 11

i want to do a square in python but im doing something wrong

I wrote this code : you enter the square width and it creates one, but when the square is created None appears too. Why?

def square(width):
    if width>=4 and width% 2 ==0:
     inferior_superior(width)
     for i in range(2):
         side(width)
     inferior_superior(width)
def inferior_superior(width):
     print("+" + "-"*(width-2) + "+")
def side(width):
    print('|' + ' '*(width-2) + '|')

Upvotes: 0

Views: 143

Answers (1)

John1024
John1024

Reputation: 113824

Observe that, at the python prompt, square works fine:

>>> square(6)
+----+
|    |
|    |
+----+

If, however, you print the result of square, you will see none:

>>> print(square(6))
+----+
|    |
|    |
+----+
None

The solution is to use square(n) by itself, not print(square(n)).

Alternative

You can assign a return value to square if you like:

>>> def square(width):
...     if width>=4 and width% 2 ==0:
...      inferior_superior(width)
...      for i in range(2):
...          side(width)
...      inferior_superior(width)
...      return "Everything works A-OK."
... 

Now, observe:

>>> print(square(6))
+----+
|    |
|    |
+----+
Everything works A-OK.

Upvotes: 3

Related Questions