验证井字游戏选项时出错



此代码要求用户提供一对坐标,然后根据有效坐标对数组进行检查。如果用户输入了无效的配对,系统会要求用户重新输入。

代码在return语句中返回错误。以下是代码和随后的错误。

def position_choice():
'''the indexes are not yet given by the user so we initialise it with a string ,
which can be anything'''
x,y="not yet","not yet"
#this contains the list of indexes that are in a tic tac  toe board
ttt_index=[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
#looping until we get the choice
while (x.isdigit()==False and y.isdigit()==False) or[int(x),int(y)] not in ttt_index:
x=(input("Which row would you like to place the element"))
y=(input("Which column in the row would like to place the element"))
if [int(x),int(y)] not in ttt_index:
clear_output()
print("I'm sorry the position that you've chosen is not valid, please do check and re-enter accordingly :)")
return int(x,y)

错误:

Traceback (most recent call last):
File "C:/Users/paul/Documents/k.py", line 16, in <module>
position_choice()
File "C:/Users/paul/Documents/k.py", line 14, in position_choice
return int(x,y)
TypeError: 'str' object cannot be interpreted as an integer

我提交了一份待批准的编辑,解释了正在发生的问题。问题是如何返回x和y。因此,将return语句更改为:return int(x), int(y)

相关内容

最新更新