-1

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:

Aurum
  • 31
  • 6

2 Answers2

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
  • 1
    With 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.

  • 1
    It 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