我需要为一款名为TicTackToe.py的游戏打印一个3 x 3阵列.我知道我们可以使用水平或垂直方式从列表中打印内容
listA=['a','b','c','d','e','f','g','h','i','j']
# VERTICAL PRINTING
for item in listA:
print item
Run Code Online (Sandbox Code Playgroud)
输出:
a
b
c
Run Code Online (Sandbox Code Playgroud)
要么
# HORIZONTAL PRINTING
for item in listA:
print item,
Run Code Online (Sandbox Code Playgroud)
输出:
a b c d e f g h i j
Run Code Online (Sandbox Code Playgroud)
如何打印两者的混合,例如打印3x3盒子
a b c
d e f
g h i
Run Code Online (Sandbox Code Playgroud) 我和我的伙伴正试图在python中创建一个有趣的游戏,其中输入数组的元素以螺旋方式访问.我尝试过几种方法,例如下面给出的方法(来源).
def spiral(X, Y):
x = y = 0
dx = 0
dy = -1
for i in range(max(X, Y)**2):
if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
print (x, y)
# DO STUFF...
if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy
Run Code Online (Sandbox Code Playgroud)
上面的语句访问螺旋循环中的元素,并为定义的数组AE打印它们.我想知道如何将给定的阵列AE转换为螺旋阵列
def __iter__(self):
return self
Run Code Online (Sandbox Code Playgroud)
只是想知道上面的代码通常做了什么,为什么需要它.
我经历了许多代码教程和块,没有任何适当的规范得到多个答案,只是一个简短的解释将是伟大的.
我想将包含 4x4 数字网格的文本文件打印到控制台,但我的代码无法正常工作。有什么建议么?
def Viewfile(self):
try:
viewFileOpen=open(self.viewFileName,'r')
for viewLine in viewFileOpen:
self.theViewBoard.append(viewLine)
print self.theViewBoard
'''
Run Code Online (Sandbox Code Playgroud)
我在这里尝试过分割和不分割
viewFileOpen=open(self.viewFileName,'r')
for viewLine in viewFileOpen:
viewListofValues=viewLine.split()
viewRow=[]
for viewItem in viewListofValues:
viewRow.append(viewItem)
self.theViewBoard.append(viewRow)
print self.theViewBoard'''
except:
print"Some Error In getting the file printed at the end"
Run Code Online (Sandbox Code Playgroud) 如果我在下面的代码中遗漏了一些东西,我有点好奇,
逻辑我希望用户输入或者"Y","H"或者"E"我希望使用return语句转移到另一个类.如果不是,我使用该else部分返回布尔值False.
有一个while循环可以将控件发送回同一个函数,直到用户输入所需的值.
在运行程序时,else无论收到什么输入,它都不会进入零件.
def getUserIngameOption(self):
userIngameOption=raw_input("Please Enter Y -> for Turn | H -> Hint | E -> to End")
userDesc=userIngameOption.upper()
if not ( userDesc==("Y") or ("H") or ("E")):
print "Mate PLEASE JUST TYPE WHATTTT is asked for !! not this",userDesc
return False
else:
self.userOpt=userDesc
print "This is detail that you got>>>",self.userOpt
return self.userOpt
Run Code Online (Sandbox Code Playgroud)