0

I'm making easy Tic Tac Toe game (not as easy as I thought it would be). I want to create a function that asks player which sign would like to play (x or o). I have a problem with returning values. It just won't return anything... like nothing has changed.

player_choice = ''
computer_choice = ''

def player_sign(player, computer):
    choice = raw_input("Do you want to be X or O?:  ").lower()
    while choice != 'x' and choice != 'o':
        print "Error!\n Wrong input!"
        choice = raw_input("Do you want to be X or Y?:  ").lower()
    if choice == 'x':
        print "X is yours!"
        player = 'X'
        computer = 'O'
        return player, computer
    elif choice == 'o':
        print "You've chosen O!"
        player = 'O'
        computer = 'X'
        return player, computer
    else:
        print "Error!\n Wrong input!"
        return 0
player_sign(player_choice, computer_choice)

Tried with samples like assigning values to player_choice and computer_choice and it prints the initial string. What am I doing wrong?

Burdzi0
  • 80
  • 1
  • 9
  • How are you calling this function? It looks like your problem is probably a misunderstanding of what parameters and return values actually are. For example, it makes no sense for `player` and `computer` to be arguments here. Arguments are used for a caller to tell a function things, not for the function to tell things to its caller. – user2357112 Jan 13 '16 at 18:28
  • Maybe it is a markdown problem, but the indention seem to be wrong. Make sure that everthing after def and while is indented by 4 spaces. – falstaff Jan 13 '16 at 18:29
  • Are you supposed to be doing something with `player_choice` and `computer_choice` ?, also the call to the function would be great – gerosalesc Jan 13 '16 at 18:31

2 Answers2

0

You need to call player_sign(player_choice,computer_choice) in your code at the end

jsidhu
  • 1
  • 3
0

You want to mutate immutable type (string) inside a function. It's not possible. For detailed explanation please refer to question: How do I pass a variable by reference?.

What you should do is simply assign return value of function to names in scope where function is called.

def player_sign():
    choice = raw_input("Do you want to be X or O?:  ").lower()
    while choice != 'x' and choice != 'o':
        print "Error!\n Wrong input!"
        choice = raw_input("Do you want to be X or Y?:  ").lower()
    if choice == 'x':
        print "X is yours!"
        player = 'X'
        computer = 'O'
        return player, computer
    elif choice == 'o':
        print "You've chosen O!"
        player = 'O'
        computer = 'X'
        return player, computer
    else:
        print "Error!\n Wrong input!"
        return None, None

player_choice, computer_choice = player_sign()
Community
  • 1
  • 1
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93