I am a beginner in python programming. I need to make a Tic Tac Toe game but where the user can input the number of the X's and the Y's.
This is my code:
def printboards():
board_s1 = " _ "
board_s2 = "| |"
board_s3 = " _ "
backspace = ("\n")
print board_s1
print board_s2
print board_s3
return
boardX = raw_input("How many X boards do you want? (insert 1 more than you want) > ")
boardY = raw_input("How many Y boards do you want? (insert 1 more than you want) > ")
for xx in range(1,int(boardX)):
printboards()
for yy in range(1,int(boardY)):
printboards()
But every time I run the program I am getting this:
And my second problem is that if I input 3 in BoardX and BoardY I am getting just 6 boxes and not 3x3 boxes.
please help
Welcome to python programming. The problem with the newline is, that every print
is done in a new line. So in your case if you call printboards()
multiple times, it will draw all the boxes under another.
You are getting only 6 boxes because the python function range()
uses a 0-index meaning that it starts counting at 0. If you would use range(0,int(boardX))
and range(0,int(boardY))
it would give you the right number of boxes. However, if you want to start at 0, you gan just discard the 0 and use range(int(boardX))
.
If you are using a function to draw the board, just let it do the entire thing:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#The function now takes the dimensions of the board in x and y as arguments
def printboards(x,y):
# This results in y rows of boxes
for i in range(y):
# with x * "some text" you can multiply text
print x*" _ "
print x*"| | "
print x*" ‾ "
boardX = raw_input("How many X boards do you want? > ")
boardY = raw_input("How many Y boards do you want? > ")
printboards(int(boardX), int(boardY))
One more thing: I also added the # -*- coding: utf-8 -*-
because i used a special character for the bottom of the box.
Hope I could help.