如何在每个oth旁边打印两个垂直列表

2024-06-08 07:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个连接4的游戏。如果你不知道规则,查一下,这是一个相对简单的游戏。我把每一行都列成一个单子。但是,它会在另一行的下面打印每一行。我想让它把每一行打印在另一行的旁边。你知道吗

#Variables that determine how close up or if the slot is full for each column
ZeroTime = 0
OneTime = 0
TwoTime = 0
ThreeTime = 0
FourTime = 0
FiveTime = 0
SixTime = 0

#Makes the List for Each Row
ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]

#Asks The Players For Their Usernames
namo = str(input("Player O what is your name: "))
namx = str(input("Player X what is your name: "))
print (namo + " is using O and " + namx + " is using X")

#Prints Current Board
print ("""
The current board is:

0 1 2 3 4 5 6
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
""")

#Asks the O player for which column they are going to choose
ot = str(input(namo + ", you're O! Enter which column you choose:"))

#Adds the slot
#For Slot 0
if ot == "0":
  ListZero [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "1":
  ListOne [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "2":
  ListTwo [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "3":
  ListThree [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1
if ot == "4":
  ListFour [7 - ZeroTime] = "O"
  print (ListZero [0 + ZeroTime])
  ZeroTime = ZeroTime + 1

else:
  print ("""We Hit an Error!
  Sorry, we don't have a slot for that. The code only allows these 
slots: 
  0, 1, 2, 3, 4, 5, 6.
 Your turn has been skipped. """) #Added turn has been skipped, I can 
fix that later but we're in the prototype right now

#Prints the Board After That
print ("""
The current board is:
""")

#I was confused on printing lists with the [] and '' so I googled 
online, the base code for this was found online. However, I added 
ListZero, ListOne, etc.
print(*ListZero, sep='\n')
print(*ListOne, sep='\n')
print(*ListTwo, sep='\n')
print(*ListThree, sep='\n')
print(*ListFour, sep='\n')
print(*ListFive, sep='\n')
print(*ListSix, sep='\n')

Tags: theforifthatiscolumnotsep
3条回答

下面是一个简短的片段,介绍如何垂直打印相邻的两个列表。也许你可以把它扩展到你的代码中。zip可以接受任何数量的支持迭代的对象。你知道吗

one = ['a', 'b', 'c']
two = [1, 2, 3]
three = ['q', 'w', 'e']

for x, y, z in zip(one,two, three):
    print(x, y, z)

输出:

a 1 q
b 2 w
c 3 e

这是你的密码对应的。。你知道吗

ListZero = ["0",".", ".", ".", ".", ".", ".", "."]
ListOne = ["1",".", ".", ".", ".", ".", ".", "."]
ListTwo = ["2",".", ".", ".", ".", ".", ".", "."]
ListThree = ["3",".", ".", ".", ".", ".", ".", "."]
ListFour = ["4",".", ".", ".", ".", ".", ".", "."]
ListFive = ["5",".", ".", ".", ".", ".", ".", "."]
ListSix = ["6",".", ".", ".", ".", ".", ".", "."]


for a,b,c,d,e,f,g in zip(ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix):
    print(a,b,c,d,e,f)

输出:

0 1 2 3 4 5
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .

希望这有帮助。你知道吗

可以使用提供多维数组的numpy数组。你知道吗

import numpy 
a = numpy.array([[YOUR LISTS],[YOUR LISTS],...])
print(a.T)

您可以先遍历列表元素的位置,然后遍历列表,而不是按顺序打印列表:

mylists = [ListZero, ListOne, ListTwo, ListThree, ListFour, ListFive, ListSix]

for i in range(8):
    for alist in mylists:
        print(alist[i], end=' ')
    print()

不过,维尼思的解决方案更为优雅。你知道吗

相关问题 更多 >

    热门问题