Python Forum
always comes up as invalid position - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: always comes up as invalid position (/thread-21736.html)



always comes up as invalid position - GalaxyCoyote - Oct-11-2019

I am trying to make a simple game, if a position has already been selected by the opposite team then it will display "invalid position", the issue is, it always displays it.

Here is my code..

def qinput():
        try:
            Pinput = int(input("Where would you like to go?: "))
        except Exception as e: #if there is an error, store it in "e"
            print(f"Invalid input {e}")

        inputloop = True
        while inputloop:
            qinput()
            if Pinput == 1 and Row1[0] == "-": #checks if the position is free
                Row1[0] = " "
                inputloop = False
            elif Pinput == 2 and Row1[1] == "-":
                Row1[1] = " "
                inputloop = False
            else:
                print("invalid position - already taken!")
                continue
if anyone has an answer or just a point in the right direction then that would be great!


RE: always comes up as invalid position - ichabod801 - Oct-11-2019

If I set Row1 = ['-', '-'], I have no problems with the code. Of course, that's because it's not really doing anything, is it? It gets the input, starts the inputloop, and calls itself, which gets the input, starts the input loop, and calls itself, and so on until you get a RecursionError.

Perhaps that is being masked by your overly broad except statement. Except statements should be as narrow as possible, in this case only caching ValueError.

Regardless of the problems of using a global variable like Row1 (pass it as a parameter to the function instead), this should be a loop:

def qinput(Row1):
    inputloop = True
    while inputloop:
        try:
            Pinput = int(input("Where would you like to go?: "))
        except ValueError:
            print('Integers only please.')
            continue
        if Pinput == 1 and Row1[0] == "-": #checks if the position is free
            Row1[0] = " "
            inputloop = False
        elif Pinput == 2 and Row1[1] == "-":
            Row1[1] = " "
            inputloop = False
        else:
            print("invalid position - already taken!")
    return Pinput
And if the existence of Row1 implies the existence of Row2 and maybe Row3, I would suggest putting them in a list.


RE: always comes up as invalid position - ichabod801 - Oct-11-2019

You might also want to check out the tutorial on Validating User Input


RE: always comes up as invalid position - GalaxyCoyote - Oct-11-2019

(Oct-11-2019, 10:35 PM)ichabod801 Wrote: If I set Row1 = ['-', '-'], I have no problems with the code. Of course, that's because it's not really doing anything, is it? It gets the input, starts the inputloop, and calls itself, which gets the input, starts the input loop, and calls itself, and so on until you get a RecursionError.

Perhaps that is being masked by your overly broad except statement. Except statements should be as narrow as possible, in this case only caching ValueError.

Regardless of the problems of using a global variable like Row1 (pass it as a parameter to the function instead), this should be a loop:

def qinput(Row1):
    inputloop = True
    while inputloop:
        try:
            Pinput = int(input("Where would you like to go?: "))
        except ValueError:
            print('Integers only please.')
            continue
        if Pinput == 1 and Row1[0] == "-": #checks if the position is free
            Row1[0] = " "
            inputloop = False
        elif Pinput == 2 and Row1[1] == "-":
            Row1[1] = " "
            inputloop = False
        else:
            print("invalid position - already taken!")
    return Pinput
And if the existence of Row1 implies the existence of Row2 and maybe Row3, I would suggest putting them in a list.

This may sound like I am an idiot but I just get this error...
TypeError: qinput() missing 1 required positional argument: 'Row1'

(Oct-11-2019, 11:29 PM)GalaxyCoyote Wrote:
(Oct-11-2019, 10:35 PM)ichabod801 Wrote: If I set Row1 = ['-', '-'], I have no problems with the code. Of course, that's because it's not really doing anything, is it? It gets the input, starts the inputloop, and calls itself, which gets the input, starts the input loop, and calls itself, and so on until you get a RecursionError.

Perhaps that is being masked by your overly broad except statement. Except statements should be as narrow as possible, in this case only caching ValueError.

Regardless of the problems of using a global variable like Row1 (pass it as a parameter to the function instead), this should be a loop:

def qinput(Row1):
    inputloop = True
    while inputloop:
        try:
            Pinput = int(input("Where would you like to go?: "))
        except ValueError:
            print('Integers only please.')
            continue
        if Pinput == 1 and Row1[0] == "-": #checks if the position is free
            Row1[0] = " "
            inputloop = False
        elif Pinput == 2 and Row1[1] == "-":
            Row1[1] = " "
            inputloop = False
        else:
            print("invalid position - already taken!")
    return Pinput
And if the existence of Row1 implies the existence of Row2 and maybe Row3, I would suggest putting them in a list.

This may sound like I am an idiot but I just get this error...
TypeError: qinput() missing 1 required positional argument: 'Row1'
Extremely sorry! I wasn't reading my code correctly, there was no issue I had just forgot to add in Row2 and Row3.