此代码要求用户提供一对坐标,然后根据一组有效坐标对进行检查。如果用户输入的一对无效,他们会被要求重新输入。
代码在 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
答案 0 :(得分:0)
我提交了一份待批准的编辑,其中解释了所发生的问题。问题是您如何返回 x 和 y。因此将返回语句更改为:return int(x), int(y)