Empty-I don't know how to delete a post

Empty-I don’t know how to delete a post

1 Like

Try arranging the input in one list:

tabledata = []

Then, start to get the input in the list:

for i in range(9):
    a = input("")
    tabledata += a

After that, start to output the data in your own.

1 Like

Can you show me an example of what I would do after

for i in range(9):
a = input("")
tabledata += a

1 Like

You could make a two dimensional list? Very similar to battleships:

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/welcome-to-battleship?action=resume_content_item

1 Like

change it to what? The code you posted is of battle ships

1 Like

I’m supposed to input the x, o, b first then display the board however in the code it displays the board every time player 1 and player 2 inputs a number. How would I change it in the battle ship code

1 Like

Recall that we concatenate lists, not values.

tabledata += [a]

versus,

tabledata.append(a)
1 Like
tablelist += a

or

tablelist.append(a)

are both equivalent.

I tested this on the shell, and worked fine.

1 Like

Then:

for i in range(3):
  print(str(tabledata[i]) + str(tabledata[i + 1]) + str(tabledata[i + 2]
  i += 1
1 Like

For the code
i += 1
Wouldn’t i be an invalid syntax

Code

1 Like
>>> a = []
>>> a += 1
Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    a += 1
TypeError: 'int' object is not iterable
>>> a.append(1)
>>> a
[1]
>>> a += [2]
>>> a
[1, 2]
>>> 
2 Likes

Ok, I’ll admit I didn’t run this directly in the shell, but I did run this in some code and there wasn’t an error with anything regarding that.

Also, I only tested this with strings, and I haven’t seen an error saying it wasn’t iterable.

1 Like

Sorry, I didn’t know that i would automatically iterate through itself. Just remove that line.

1 Like

It is this form that I refer to in your earlier example, the one which raised the concern, in the first place.


>>> a = []
>>> a += "A"
>>> a
['A']
>>> 

I get it now. We have seen two sides of the die. And lo, here is yet another…

>>> a = 'A'
>>> a += ['abc']
Traceback (most recent call last):
  File "<pyshell#86>", line 1, in <module>
    a += ['abc']
TypeError: Can't convert 'list' object to str implicitly
>>> 

It would appear that we need to know and understand limitations so we don’t make any false assumptions in our code design. It would also mean that when there are certain ambiguities about code behavior, we ought to favor instead the more explicit approach at hand. The reader is less likely to make assumptions of their own.

1 Like

When I remove that line of code, it now shows unexpected EOF while parsing

Code

1 Like

Do you know what EOF error means? Have you tried googling what it is, and what might cause it?

1 Like

It means when there’s no data given when calling input

1 Like

What does the abbreviation EOF even stand for?

that is not the only case which can throw an EOF error. I recommend to read a bit more about EOF errors

1 Like

Thank you, @mtf. I understand now about iterating and how it works now.

For strings being added to a list, we use this:

a += "b"

And for integers being added to a list, we use this:

a += [2]

I guess we’re both on the same page now.

1 Like

Moreover, it technically means that you’re doing some sort of syntax that is wrong to the actual syntax of Python. You’re kind of discontinuing it.

1 Like