我想做五英寸,我创建了两个函数,axe()创建游戏数组的轴,area()根据指定的坐标插入“X”。你知道吗
fiveinches = []
def axe():
for a in range(1, 10):
x = []
x.append(a)
print("", a, end="")
for b in range(1, 10):
y = []
if b == 1:
y.append(b)
print("\n", b, sep="")
else:
print(b)
def area():
for i in range(1,10):
temp = []
for j in range(1,10):
temp.append(" ")
fiveinches.append(temp)
fiveinches[n][m] = "X"
for list in twins:
for element in list:
print(element, end="")
print(end="\n")
print("there is a player with crosses")
n = int(input("Enter the coordinate X: "))
m = int(input("Enter the coordinate Y: "))
#axe() or area()
问题是,我现在不知道如何将这两个函数耦合起来,使它们都绘制在一个区域中。你知道吗
与其使用两个函数分别打印输出的一部分,不如使用一个函数打印整个输出。您可以在全局范围中存储一个字符网格,这样就可以由用户输入写入,并由display函数读取。你知道吗
结果:
如果你必须完全保持你的功能不变,那么你将面临一场艰难的战斗。一旦将数据打印到控制台,Python就几乎无法访问或操作它。当
area
执行时,axe
的输出也可能落入黑洞。你知道吗相反,这就是defaultstdout类文件对象的行为方式。我们可以根据需要交换print to console行为;例如,内置的
io.StringIO
类可以静默地捕获axe()和area()
的输出。然后我们可以把它们组合起来,打印成普通的标准输出。你知道吗你几乎不想真的这么做。当数据无法以任何其他方式检索时,捕获标准输出是一种绝望的策略。您可以完全控制您的数据,因此您应该首先尝试其他十几种方法。但既然你问:
结果:
我注意到X并没有完全与轴对齐-它不是与左边的8和上面的2对齐,而是与左边的2和上面的4.5对齐。也许您可以修改
fiveinches[n][m] = "X"
,以便将字符放置在正确的位置。但是我答应过不会改变area
超出严格必要的范围,所以更新这一行取决于您;-)(*除了在
area
中将twins
更改为fiveinches
之外,因为twins
未定义)相关问题 更多 >
编程相关推荐