Posts: 44
Threads: 25
Joined: Apr 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!
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
You might also want to check out the tutorial on Validating User Input
Posts: 44
Threads: 25
Joined: Apr 2019
Oct-11-2019, 11:29 PM
(This post was last modified: Oct-11-2019, 11:38 PM by GalaxyCoyote.)
(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.
|