学习用Python编程。我正在尝试使用海龟制作 XOX 游戏。我做了一个函数来检查列表中是否有 X(x) 或 O(y),如果有的话,我将放置 X 或 O。 该函数运行良好,直到我试图查找列表中是否没有空引号,但我猜它没有像我想象的那样工作。
部分问题出现的地方:
from turtle import *
list1 = [""] * 9
if (
# If there are no empty strings in the list, decide a draw.
not list1[0] == ""
and not list1[1] == ""
and not list1[2] == ""
and not list1[3] == ""
and not list1[4] == ""
and not list1[5] == ""
and not list1[6] == ""
and not list1[7] == ""
and not list1[8] == ""
):
print("It's a tie.")
replay()
整体功能:
from turtle import *
list1 = [""] * 9
def goplace(x, y):
pencolor("blue")
sq_1 = 0 < x < 1 and 0 < y < 1
sq_2 = 0 < x < 1 and 1 < y < 2
sq_3 = 0 < x < 1 and 2 < y < 3
sq_4 = 1 < x < 2 and 0 < y < 1
sq_5 = 1 < x < 2 and 1 < y < 2
sq_6 = 1 < x < 2 and 2 < y < 3
sq_7 = 2 < x < 3 and 0 < y < 1
sq_8 = 2 < x < 3 and 1 < y < 2
sq_9 = 2 < x < 3 and 2 < y < 3
count_x = 0
count_y = 0
for item_x in list1:
if item_x == "x":
count_x += 1
for item_y in list1:
if item_y == "y":
count_y += 1
if count_x <= count_y:
if sq_1:
if list1[0] == "":
list1[0] = "x"
print(list1)
draw_x(0, 0)
else:
return
elif sq_2:
if list1[1] == "":
list1[1] = "x"
print(list1)
draw_x(0, 1)
else:
return
elif sq_3:
if list1[2] == "":
list1[2] = "x"
print(list1)
draw_x(0, 2)
else:
return
elif sq_4:
if list1[3] == "":
list1[3] = "x"
print(list1)
draw_x(1, 0)
else:
return
elif sq_5:
if list1[4] == "":
list1[4] = "x"
print(list1)
draw_x(1, 1)
else:
return
elif sq_6:
if list1[5] == "":
list1[5] = "x"
print(list1)
draw_x(1, 2)
else:
return
elif sq_7:
if list1[6] == "":
list1[6] = "x"
print(list1)
draw_x(2, 0)
else:
return
elif sq_8:
if list1[7] == "":
list1[7] = "x"
print(list1)
draw_x(2, 1)
else:
return
elif sq_9:
if list1[8] == "":
list1[8] = "x"
print(list1)
draw_x(2, 2)
else:
return
if not (
list1[0] == list1[1] == list1[2] == "x"
or list1[3] == list1[4] == list1[5] == "x"
or list1[6] == list1[7] == list1[8] == "x"
or list1[0] == list1[4] == list1[8] == "x"
or list1[2] == list1[4] == list1[6] == "x"
or list1[0] == list1[1] == list1[2] == "y"
or list1[3] == list1[4] == list1[5] == "y"
or list1[6] == list1[7] == list1[8] == "y"
or list1[0] == list1[4] == list1[8] == "y"
or list1[2] == list1[4] == list1[6] == "y"
):
r_place()
if list1[0] == list1[1] == list1[2] == "x":
print("Player won!")
replay()
elif list1[3] == list1[4] == list1[5] == "x":
print("Player won!")
replay()
elif list1[6] == list1[7] == list1[8] == "x":
print("Player won!")
replay()
elif list1[0] == list1[1] == list1[2] == "y":
print("Computer won!")
replay()
elif list1[3] == list1[4] == list1[5] == "y":
print("Computer won!")
replay()
elif list1[6] == list1[7] == list1[8] == "y":
print("Computer won!")
replay()
elif list1[0] == list1[4] == list1[8] == "y":
print("Computer won!")
replay()
elif list1[2] == list1[4] == list1[6] == "y":
print("Computer won!")
replay()
elif list1[0] == list1[4] == list1[8] == "x":
print("Player won!")
replay()
elif list1[2] == list1[4] == list1[6] == "x":
print("Player won!")
replay()
if (
not list1[0] == ""
and not list1[1] == ""
and not list1[2] == ""
and not list1[3] == ""
and not list1[4] == ""
and not list1[5] == ""
and not list1[6] == ""
and not list1[7] == ""
and not list1[8] == ""
):
print("It's a tie.")
replay()
我尝试使用“!=”,“and”,“not,in,“None”,“''”而不是==“”并尝试仅list1 [],但它们都不适合我。
您需要使用
!=
来测试列表项与空字符串的相等性 ""
如:
list1 = ["a"] * 9
# If there are no empty strings in the list, decide a draw.
if (
list1[0] != ""
and list1[1] != ""
and list1[2] != ""
and list1[3] != ""
and list1[4] != ""
and list1[5] != ""
and list1[6] != ""
and list1[7] != ""
and list1[8] != ""
):
print("Replay!")