I have a problem with programming "Reversi" game in Python. I want the size of the game board to be optional, so the user can request for example a 4x4 or 10x10 (more than that isn't nescesary). But as I tried to code a:
Asked
Active
Viewed 478 times
-1
-
Is your indentation in the original code the same as in the pasted one? then it won't work just for that reason. – Eugene Sh. Jul 27 '17 at 16:42
-
The code you show is both poorly indented *and* fails to mention `Q` at all. – chepner Jul 27 '17 at 16:42
-
Sorry I accidently posted an old code – Aurum Jul 27 '17 at 16:44
-
It is still not indented properly. – Eugene Sh. Jul 27 '17 at 16:45
-
What do you mean? – Aurum Jul 27 '17 at 16:45
-
this is just a short bit of my complete code – Aurum Jul 27 '17 at 16:46
-
You know what "indentation" means, right? And you know it's importance in Python? – Eugene Sh. Jul 27 '17 at 16:46
-
1Yes and I do have it in my code, somehow when I pasted it here it went flat, that is not the problem but the optional size of my board – Aurum Jul 27 '17 at 16:48
-
2So where is `Q` in this code? – Eugene Sh. Jul 27 '17 at 16:53
-
sorry I accepted an edit, which made the Q's dissapear again. Now it's updated! – Aurum Jul 27 '17 at 17:03
-
Pass `Q` as an input argument of your function. As it is `Q` is not defined in the function `getNewBoard()` for example (nor in the following function). – Eskapp Jul 27 '17 at 18:08
-
And I think that you do not want the size of the board to be `optional` but rather `personalized` or `customizable` from the way you present your goal. – Eskapp Jul 27 '17 at 18:14
-
can I ask why you just deleted all of the question and unaccepted the answer? – Uvar Jul 27 '17 at 21:15
-
There is no code in your question and it ends on an incomplete sentence. Please add all relevant details to your post. – SandPiper Aug 01 '17 at 12:29
2 Answers
1
There are multiple things off in this question.
It cannot be verified where you define Q. Seeing from the error thrown, you are probably defining it in a local scope. Q will then only exist in this local scope.
To now see what happens:
def foo():
Q = input("which size of board would you like? for example a 4x4 is a 4")
print(Q)
foo()
print(Q)
>> which size of board would you like? for example a 4x4 is a 48
>> 8
>> Traceback (most recent call last):
File "<ipython-input-37-56b566886820>", line 1, in <module>
runfile('C:/Users/idh/stacktest.py', wdir='C:/Users/idh')
File "c:\users\idh\appdata\local\continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 688, in runfile
execfile(filename, namespace)
File "c:\users\idh\appdata\local\continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/idh/stacktest.py", line 15, in <module>
print(Q)
NameError: name 'Q' is not defined
Your way of defining Q will return a string, which will break the rest of your code anyway.
Q = int(input("Which size of board would you like?")
for i in range(Q):
print(i)
>> Traceback (most recent call last):
File "<ipython-input-39-5abb59a2214a>", line 1, in <module>
for i in range(Q):
TypeError: 'str' object cannot be interpreted as an integer
Try something like the below:
try:
Q = int(input("Which size of board would you like? For example, a 4x4 board is a 4 \n\n >>"))
except:
print("Requires an integer between 4 and 10")
Q = int(input("Which size of board would you like? For example, a 4x4 board is a 4 \n\n >>"))
def whatever_function1(*args, **kwargs):
whatever it is supposed to do
return whatever it is supposed to return
def whatever_function2(*args, **kwargs):
whatever it is supposed to do
return whatever it is supposed to return
etc
alternatively, you can manually pass Q through to each function after defining it:
Q = int(input("What size would you like?\n")
def getNewBoard(Q):
# Creates a brand new, blank board data structure.
board = []
for i in range(Q):
board.append([' '] * Q)
return board

Uvar
- 3,372
- 12
- 25
-
1With respect to the fact the question asked is very basic, I imagine the `*args` and `**kwargs` mentions may be hard to understand for people reading this good answer. So here is the link to learn more about these concepts :) https://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-parameters – Eskapp Jul 27 '17 at 18:19
0
You need to use global to tell python that you want to use the global value of Q. To do that just write global Q in the functions where you use that variable.

Covor Sorin
- 26
- 4
-
1It is not a good practice in general to have global variables in Python except when it is really needed and justified. Better practice in this case I believe would be to simply pass `Q` as an input argument of the functions that need this variable. – Eskapp Jul 27 '17 at 18:10