2

I'm new to python. Started making a tic tac toe game.

I've printed a board, so that's not the concern. My function play_sequence has something weird happening.

from IPython.display import clear_output

def display_board(board):
    print(board[7]+  '|'+board[8]+  '|' +board[9])
    print('----')
    print(board[4]+  '|'+board[5]+  '|' +board[6])
    print('----')
    print(board[1]+  '|'+board[2]+  '|' +board[3])

def play_sequence():
    move1 = ''

    while move1 != range(0,10,1):
        move1 = input("Player 1, choose a number:")
        break

    while move1 == input(move1):
        print("You chose 1")

I want the player to choose a number, and it should return you chose 1, and then the board with a 1 on board[1] on the board. Instead, I put in 1, and then a new line appears with a 1 in front of a box to put in another input. Any ideas? Then I put in another 1, and then it shows '11.' This is funny, but I need help. Any ideas?

Airmastix
  • 21
  • 1
  • 3
    Do you mean `move1 not in range(0,10,1)`? Right now, you're comparing a number (string) to a whole range. Also, you'll need to use `int` to turn the entered string number into an actual number. Comparing a string to a number will always be false. – Carcigenicate Jan 06 '19 at 02:00
  • [Possible dupe](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – DYZ Jan 06 '19 at 03:10

3 Answers3

0

I'm not quite sure why you are using while loops. I believe you want to ask if move1 in range(1, 10) (by the way no need to write range(0, 10, 1) because step is by default 1, and also your tic tac toe board goes from 1 to 9 not from 0 hence range(1, 10)). This would be like saying "if move1 is not a number between 1 and 9, then ask for an input". I think what you mean is also if move1 == ''. There is also a small problem with this, which is that inputs are strings... so they can't really be compared with a number from the range. i.e. '3' != 3.

To easily change that, you can just do int(input(".. whatever you ask for ..")) which converts the string into integers.

Just a side note but when you write move1 = '', I know you're using empty string because you don't have anything, but you can also use move1 = None. Then you could also say if not move1: instead ("None" is False, so it's like saying if not False i.e if True).

Up until now, you've got:

def play_sequence():
    move1 = None

    if not move1:
        move1 = int(input("Player 1, choose a number:"))

Also, you could make it much easier by just saying

    def play_sequence():
        move1 = int(input("Player 1, choose a number:"))

because there isn't much point in making move1 when you are going to change it anyway. Unless I didn't quite understand why you were using the while loop... I guess you wanted to check whether the input is a number between 1-9?

Anyway, the problem you found with the weird "1 1 1" stems from this:

while move1 == input(move1): print("You chose 1")

Here, you use input(), and whenever you have input(), you actually ask for an input. That's why you're putting 1 again and again. If you write 2 or some other number for example, then the while loop stops. I'm actually not quite sure what you are doing here, but I think what you were trying to say is:

print("You chose", move1)

This would print the number which was chosen in the first part of your function.

Hence:

def play_sequence():
    move1 = None

    if not move1:
        move1 = int(input("Player 1, choose a number:"))

    print("You chose", move1)

I hope I'm helping and I didn't confuse you... :) Have fun learning python though!

lia
  • 21
  • 1
  • `move1 = None` followed by `if not move1` is nonsense. – DYZ Jan 06 '19 at 03:06
  • Yes, but I thought that is what Airmastix meant with `move1 = ''`, so I tried to keep it close to this original code... :) – lia Jan 06 '19 at 03:08
  • Thanks. It’s works now. Now I need to print the board with an x on the spot where the 1 is – Airmastix Jan 06 '19 at 18:22
0

Actually I'm guessing if you really want to check whether the input is a number between 1 and 9, you can add this.

def play_sequence():
    move1 = int(input("Player 1, choose a number:"))

    # checking
    while move1 not in range(1, 10):
        move1 = int(input("Player 1, the number must be between 1 and 9"))

    print("You chose", move1)

Just one final point - I'm wondering whether you're going to make a super long function with all the repeats of the playing sequence... i.e. are you going to put move2, move3, ....? I think it would be best to use a big while loop for that. But I guess if you just want to practice for now, just have fun with it :)

lia
  • 21
  • 1
0

I think you can try this

def play_sequence():
    while True:
        move = int(input("Player 1, choose a number:"))
        if move not in range(0, 10, 1):
            break
        print("Your choice is", move)
musu
  • 9
  • 2