Your functions are not returning anything so when you set a variable using randow_row or random_col the variable is set to nothing. Also when you call random_row and random_col you're forgetting to pass in the parameter.
That should help!
thanks. i left return column out of my code.
Where did you get ship_row and ship_col from
Your way worked for me, jj791762!
I am also wondering the same thing myself
I guess they had to make it up so it would work.
Ok, so you have a few things going on here that is making your code too complicated and also has unnecessary parts. Also the reason why it does not work is because you never change the scope the data is at.
When you create a function you are creating something at the local level of the function which the global level will not be able to access once the object no longer exists. So if you never return the data to the global or to the caller you will forever lose the data that you generated/captured.
So remember that scope is really important in python as it is in most languages. If you do not give the code the information you need there is no way for it to access it.
EXAMPLE of SCOPE Issues!
apple = "APPLE" def check_apple(apple_test): if apple_test == "APPLE": apple = 1 check_apple(apple_test) print(apple_test) # OUTPUT: 'APPLE'
The reason it never actually changes the local variable even though we are directly using the object apple and not using a reference is because when we try to use the object in a new scope the python interpreter automagically goes OK, there is a reference to this object here but there is not anything by this namespace at this scope level. It then creates a new object with the given name and then it works on that object.
We can get this function to work correctly by telling python that we intend to use the global scope of the variable.
apple = "APPLE" def check_apple(apple_test): global apple if apple_test == "APPLE": apple = 1 check_apple(apple_test) print(apple_test) # OUTPUT: 1
There, now the function does what you think it would. If any part of scope is confusing ask and I will try to help further.
The reason why this is important is because even though you created new variables at the local level of the function they stay at that level and when python is done with the function it preforms garbage collection on the objects and they disappears.
Also another thing you guys/gals will want to keep in mind is that the more code that does nothing useful in your program the harder it will be to maintain and decipher later.
Now, let me provide you with a new version of your code.
PYTHON CODE: BATTLESHIP
from random import randint # This will do what you are doing but return a tuple we can use. def generate_cord(): return randint(0, 5), randint(0, 5) # This is called unpacking assignment here, you can assign variables as long as you have data left. ship_row, ship_col = generate_cord()
You do not have to generate cords like this but it is a short version of what you have going on, and because it is compartmentalized you can reuse this code over and over with out worrying about corrupting your variable data.
As always if there are any questions ask freely, GOOD luck!