Python Forum
always comes up as invalid position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
always comes up as invalid position
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You might also want to check out the tutorial on Validating User Input
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 4,700 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,348 Sep-18-2020, 10:10 PM
Last Post: tienttt
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,739 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,471 Feb-08-2020, 06:58 PM
Last Post: karkas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020