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
#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


Messages In This Thread
RE: always comes up as invalid position - by ichabod801 - Oct-11-2019, 10:35 PM

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 5,144 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,696 Sep-18-2020, 10:10 PM
Last Post: tienttt
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,929 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,824 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