-1

I have this code and it returns "TypeError: cannot concatenate 'str' and 'int' objects". The 'number' ints are global variables.

print (str("+---+---+---+"))
print (str("| ")) + (int(number1)) + (str(" | ")) + (int(number2)) + (str(" | ")) + (int(number3)) + (str("|"))
print (str("+---+---+---+"))
print (str("| ")) + (int(number4)) + (str(" | ")) + (int(number5)) + (str(" | ")) + (int(number6)) + (str("|"))
print (str("+---+---+---+"))
print (str("| ")) + (int(number7)) + (str(" | ")) + (int(number8)) + (str(" | ")) + (int(number9)) +(str("|"))
print (str("+---+---+---+"))

The code should print out something like this.

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
m.wasowski
  • 6,329
  • 1
  • 23
  • 30
user3448743
  • 43
  • 3
  • 10

4 Answers4

0

Your code must to be like this:

print (str("+---+---+---+"))
print (str("| ")) + (str(number1)) + (str(" | ")) + (str(number2)) + (str(" | ")) + (str(number3)) + (str("|"))
print (str("+---+---+---+"))
print (str("| ")) + (str(number4)) + (str(" | ")) + (str(number5)) + (str(" | ")) + (str(number6)) + (str("|"))
print (str("+---+---+---+"))
print (str("| ")) + (str(number7)) + (str(" | ")) + (str(number8)) + (str(" | ")) + (str(number9)) +(str("|"))
print (str("+---+---+---+"))

I suggest to you use some variables and delete some parentheses, like this:

line = "+---+---+---+"
pipe = "| "

print line
print pipe + str(number1) + pipe + str(number2) + pipe + str(number3) + pipe
print line
print pipe + str(number4) + pipe + str(number5) + pipe + str(number6) + pipe
print line
print pipe + str(number7) + pipe + str(number8) + pipe + str(number9) + pipe 
print line
guisantogui
  • 4,017
  • 9
  • 49
  • 93
-1

In Python there is not implicit conversion between types, so language requires you to explicitly decide what exactly are you going to do. This helps to avoid many hard-to debug errors later and I consider it great advantage of Python.

You need to cast your numbers to strings, using built-in str(your_number). It would be also good to clear up your code a little...

numbers = range(1,10)
number1, number2, number3, \
    number4, number5, number6, \
    number7, number8, number9 = numbers

print ("+---+---+---+")
print ("| " + str(number1) + " | " + str(number2) + " | " + str(number3) + " |")
print ("+---+---+---+")
print ("| " + str(number4) + " | " + str(number5) + " | " + str(number6) + " |")
print ("+---+---+---+")
print ("| " + str(number7) + " | " + str(number8) + " | " + str(number9) + " |")
print ("+---+---+---+")

Also, it is not good idea to make variable for each number, better to use container like list. List of numbers from 1 to 10 you can make with range(1,10). You can later use it like array.

We can make your code even simpler with use of string substitution:

numbers = range(1,10)
horizontal_sep = "+---+---+---+"    

print (horizontal_sep)
print ("| {} | {} | {} |".format(*numbers[0:3]))
print (horizontal_sep)
print ("| {} | {} | {} |".format(*numbers[3:6]))
print (horizontal_sep)
print ("| {} | {} | {} |".format(*numbers[6:9]))
print (horizontal_sep)

Here we mark with {} places where we insert variables, that we pass inside format(). We put there slices of our number list. Slice is also a list, so we unpack it with * operator, so each 3-elements long list gets upacked to 3 integers.

Finally, we can see that our slices have some common pattern in their indexes. We can generate it easily using zip() and range() builtins, and use it for loop:

numbers = range(1,10)
horizontal_sep = "+---+---+---+"    

# zip(...) generates pairs (0, 3) (3, 6) (6, 9)
for start, end in zip(range(0, 10, 3), range(3, 10, 3)): 
    print (horizontal_sep)
    print ("| {} | {} | {} |".format(*numbers[start:end]))
print (horizontal_sep)
m.wasowski
  • 6,329
  • 1
  • 23
  • 30
-1

You can just convert to a string and then join them with the separatro and print that, like so:

nums = [1,2,3]
print ' | '.join( map(str, nums) )

In fact, when you have a 2-d list:

allNums = [[1,2,3], [4,5,6], [7,8,9]]
numPrnt = ['| ' + ' | '.join( map(str, nums) ) + ' |' for nums in allNums]
separator = '+---+---+---+\n'
ends      = '+---+---+---+\n'

print ends, separator.join( numPrint ), ends,

Shouls be able to print things for you ...

ssm
  • 5,277
  • 1
  • 24
  • 42
-1

You seem to be confused about what the functions int and str do. It's actually just the reverse of how you're using them: int converts a string to an integer, and str converts a number (or anything else) to a string.

So, since you want everything to be a string to concatenate them, you should have used str(integer1), etc, and just " | " is enough (rather than str(" | ")), since " | " is already a string.

Also, you don't need to start every print argument with ``str(...), since they are already strings. So the first line would be:

print("+---+---+---+")

and so on.

Don O'Donnell
  • 4,538
  • 3
  • 26
  • 27