Pass a variable between multiple several functions | Python 3
thanks for reading this post. I want to make an advanced TicTacToe game with AI and other stuff. I need to pass the spots(s1-s9) variable between different functions. I have been researching for quite a bit now, and I would like to meet an answer. Here is part of the code I need to execute:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9
def print_spots():
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
set_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
I want to be able to set the spots in any function like if I had a turnX function. Like if I had:
def turnx(): #This isnt in this code though
#if stuff == other stuff (just example):
set_spots('X','O',3,4,5,6,7,8,9)
But the output is:
NameError: name 's1' is not defined
So basically, I need the program to ask the user where their x or o would be placed on the board (which you don't have to worry about) then have that value stored to be printed out. Like if I change 1 to X in the game, it needs to be stored so it can be printed out.
python python-3.x variables
add a comment |
thanks for reading this post. I want to make an advanced TicTacToe game with AI and other stuff. I need to pass the spots(s1-s9) variable between different functions. I have been researching for quite a bit now, and I would like to meet an answer. Here is part of the code I need to execute:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9
def print_spots():
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
set_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
I want to be able to set the spots in any function like if I had a turnX function. Like if I had:
def turnx(): #This isnt in this code though
#if stuff == other stuff (just example):
set_spots('X','O',3,4,5,6,7,8,9)
But the output is:
NameError: name 's1' is not defined
So basically, I need the program to ask the user where their x or o would be placed on the board (which you don't have to worry about) then have that value stored to be printed out. Like if I change 1 to X in the game, it needs to be stored so it can be printed out.
python python-3.x variables
add a comment |
thanks for reading this post. I want to make an advanced TicTacToe game with AI and other stuff. I need to pass the spots(s1-s9) variable between different functions. I have been researching for quite a bit now, and I would like to meet an answer. Here is part of the code I need to execute:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9
def print_spots():
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
set_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
I want to be able to set the spots in any function like if I had a turnX function. Like if I had:
def turnx(): #This isnt in this code though
#if stuff == other stuff (just example):
set_spots('X','O',3,4,5,6,7,8,9)
But the output is:
NameError: name 's1' is not defined
So basically, I need the program to ask the user where their x or o would be placed on the board (which you don't have to worry about) then have that value stored to be printed out. Like if I change 1 to X in the game, it needs to be stored so it can be printed out.
python python-3.x variables
thanks for reading this post. I want to make an advanced TicTacToe game with AI and other stuff. I need to pass the spots(s1-s9) variable between different functions. I have been researching for quite a bit now, and I would like to meet an answer. Here is part of the code I need to execute:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9
def print_spots():
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
set_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
I want to be able to set the spots in any function like if I had a turnX function. Like if I had:
def turnx(): #This isnt in this code though
#if stuff == other stuff (just example):
set_spots('X','O',3,4,5,6,7,8,9)
But the output is:
NameError: name 's1' is not defined
So basically, I need the program to ask the user where their x or o would be placed on the board (which you don't have to worry about) then have that value stored to be printed out. Like if I change 1 to X in the game, it needs to be stored so it can be printed out.
python python-3.x variables
python python-3.x variables
edited Nov 26 '18 at 1:57
NarrowGlint
asked Nov 25 '18 at 21:46
NarrowGlintNarrowGlint
13
13
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
i believe you should try using the variables from set spots, on print spots:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
if that doesen't work then i'm not sure
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
add a comment |
You can do this without the need of the unnecessary set_spots() function.
All you need is an array:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
This outputs:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
add a comment |
do yourself a favor and represent the spots as a list. Then store that list in a variable when you call set spots and pass it on to print:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return [s1,s2,s3,s4,s5,s6,s7,s8,s9]
def print_spots( spots ):
s1,s2,s3,s4,s5,s6,s7,s8,s9 = spots
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
spots = set_spots(1,2,3,4,5,6,7,8,9)
print_spots(spots )
game_loop()
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472312%2fpass-a-variable-between-multiple-several-functions-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
i believe you should try using the variables from set spots, on print spots:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
if that doesen't work then i'm not sure
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
add a comment |
i believe you should try using the variables from set spots, on print spots:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
if that doesen't work then i'm not sure
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
add a comment |
i believe you should try using the variables from set spots, on print spots:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
if that doesen't work then i'm not sure
i believe you should try using the variables from set spots, on print spots:
def print_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return s1,s2,s3,s4,s5,s6,s7,s8,s9):
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
print_spots(1,2,3,4,5,6,7,8,9)
print_spots()
game_loop()
if that doesen't work then i'm not sure
answered Nov 25 '18 at 21:56
Theo BostonTheo Boston
115
115
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
add a comment |
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
1
1
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
That wouldnt make sense because you cant type code after a return statement
– NarrowGlint
Nov 25 '18 at 23:42
add a comment |
You can do this without the need of the unnecessary set_spots() function.
All you need is an array:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
This outputs:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
add a comment |
You can do this without the need of the unnecessary set_spots() function.
All you need is an array:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
This outputs:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
add a comment |
You can do this without the need of the unnecessary set_spots() function.
All you need is an array:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
This outputs:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
You can do this without the need of the unnecessary set_spots() function.
All you need is an array:
def turnx(s):
if 1 == 1: # (a condition):
s = ['X','O',3,4,5,6,7,8,9]
return s
def print_spots(s):
print('n')
print(str(s[0]) + ' | ' + str(s[1]) + ' | ' + str(s[2]))
print('--+---+--')
print(str(s[3]) + ' | ' + str(s[4]) + ' | ' + str(s[5]))
print('--+---+--')
print(str(s[6]) + ' | ' + str(s[7]) + ' | ' + str(s[8]))
def game_loop():
spots = [1,2,3,4,5,6,7,8,9] # Use an array
print_spots(spots)
spots = turnx(spots)
print_spots(spots)
game_loop()
This outputs:
1 | 2 | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
X | O | 3
--+---+--
4 | 5 | 6
--+---+--
7 | 8 | 9
answered Nov 25 '18 at 21:59
Suraj KothariSuraj Kothari
972619
972619
add a comment |
add a comment |
do yourself a favor and represent the spots as a list. Then store that list in a variable when you call set spots and pass it on to print:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return [s1,s2,s3,s4,s5,s6,s7,s8,s9]
def print_spots( spots ):
s1,s2,s3,s4,s5,s6,s7,s8,s9 = spots
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
spots = set_spots(1,2,3,4,5,6,7,8,9)
print_spots(spots )
game_loop()
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
add a comment |
do yourself a favor and represent the spots as a list. Then store that list in a variable when you call set spots and pass it on to print:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return [s1,s2,s3,s4,s5,s6,s7,s8,s9]
def print_spots( spots ):
s1,s2,s3,s4,s5,s6,s7,s8,s9 = spots
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
spots = set_spots(1,2,3,4,5,6,7,8,9)
print_spots(spots )
game_loop()
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
add a comment |
do yourself a favor and represent the spots as a list. Then store that list in a variable when you call set spots and pass it on to print:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return [s1,s2,s3,s4,s5,s6,s7,s8,s9]
def print_spots( spots ):
s1,s2,s3,s4,s5,s6,s7,s8,s9 = spots
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
spots = set_spots(1,2,3,4,5,6,7,8,9)
print_spots(spots )
game_loop()
do yourself a favor and represent the spots as a list. Then store that list in a variable when you call set spots and pass it on to print:
def set_spots(s1, s2, s3, s4, s5, s6, s7, s8, s9):
return [s1,s2,s3,s4,s5,s6,s7,s8,s9]
def print_spots( spots ):
s1,s2,s3,s4,s5,s6,s7,s8,s9 = spots
print('n')
print(str(s1) + ' | ' + str(s2) + ' | ' + str(s3))
print('--+---+--')
print(str(s4) + ' | ' + str(s5) + ' | ' + str(s6))
print('--+---+--')
print(str(s7) + ' | ' + str(s8) + ' | ' + str(s9))
def game_loop():
spots = set_spots(1,2,3,4,5,6,7,8,9)
print_spots(spots )
game_loop()
answered Nov 25 '18 at 21:51
Christian SloperChristian Sloper
1,379213
1,379213
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
add a comment |
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
Thanks for answering I will look at it later I have to go at the moment.
– NarrowGlint
Nov 25 '18 at 22:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472312%2fpass-a-variable-between-multiple-several-functions-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown