简体   繁体   中英

I no longer have an alignment issue, however, when I print out my X's and O's there will be a 0 next to them. Does anyone know a different solution?

This is what I have so far, if you have any ideas please let me know. It would mean a lot to me.

a_list = list(range(1, squared_input + 1))
turn = 0
Symbol_1 = "X"
Symbol_2 = "O"

while turn <= 9:
  X = 1
  while X < squared_input + 1 :
    print(str(a_list[X - 1]).zfill(2), end= "")
    if X%board_size == 0 :
      print("")
      print(("--+" * (board_size - 1)), end="")
      print("--")
    else:
      print("|", end="")
    X = X + 1
  turn = turn + 1
  Symbol_1, Symbol_2 = Symbol_2, Symbol_1
  print("You are user " + Symbol_1 + ".")
  user_input = input("Please pick a slot on the game board (using numbers 1 - " + str(squared_input) + "): ")
  a_list[int(user_input) - 1] = Symbol_1

The zeros come from your call to zfill which explicitly pads a string with 0 to a requested size. You call zfill(2) with a string that contains a single character. So the function pads that to length two by adding a 0 .

To pad with blanks you can for example use the format() function or just something like

'%2d' % a_list[X-1]

which will pad each number to length 2 from the left with blanks, or

'%-2d' % a_list[X-1]

which will pad each number to length 2 from the right with blanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM