The while loop is not repeating itself upon throwing garbage values
I am trying to run a while loop using the 'not' keyword but when I am doing so, the loop is accepting garbage values as well(for eg. bjskdjb) which is not the condition set for the while/for loop.
When I am running the for loop instead, it is throwing a syntax error.
What are the mistakes I am doing here(both using for loop and while loop) ?
I have tried doing it with for loop and while loop.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
# Using For loop I am getting a syntax error.
def askplayerchoice():
playerchoice = ''
for playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
I am expecting the code to keep asking the user to enter either 'X' or 'O' until the user enters correct values.
python python-3.x
add a comment |
I am trying to run a while loop using the 'not' keyword but when I am doing so, the loop is accepting garbage values as well(for eg. bjskdjb) which is not the condition set for the while/for loop.
When I am running the for loop instead, it is throwing a syntax error.
What are the mistakes I am doing here(both using for loop and while loop) ?
I have tried doing it with for loop and while loop.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
# Using For loop I am getting a syntax error.
def askplayerchoice():
playerchoice = ''
for playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
I am expecting the code to keep asking the user to enter either 'X' or 'O' until the user enters correct values.
python python-3.x
add a comment |
I am trying to run a while loop using the 'not' keyword but when I am doing so, the loop is accepting garbage values as well(for eg. bjskdjb) which is not the condition set for the while/for loop.
When I am running the for loop instead, it is throwing a syntax error.
What are the mistakes I am doing here(both using for loop and while loop) ?
I have tried doing it with for loop and while loop.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
# Using For loop I am getting a syntax error.
def askplayerchoice():
playerchoice = ''
for playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
I am expecting the code to keep asking the user to enter either 'X' or 'O' until the user enters correct values.
python python-3.x
I am trying to run a while loop using the 'not' keyword but when I am doing so, the loop is accepting garbage values as well(for eg. bjskdjb) which is not the condition set for the while/for loop.
When I am running the for loop instead, it is throwing a syntax error.
What are the mistakes I am doing here(both using for loop and while loop) ?
I have tried doing it with for loop and while loop.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
# Using For loop I am getting a syntax error.
def askplayerchoice():
playerchoice = ''
for playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
else:
return ['O', 'X']
askplayerchoice()
'''
I am expecting the code to keep asking the user to enter either 'X' or 'O' until the user enters correct values.
python python-3.x
python python-3.x
asked Jan 1 at 5:46


Prashant MishraPrashant Mishra
345
345
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You are returning from the loop to the function caller in the first pass itself, because you use return
inside loop. Hence, whatever is the value other than 'X'
, ['O', 'X']
is returned.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You can also make use of an infinite loop while True:
here, like so:
def askplayerchoice():
playerchoice = ''
while True:
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
@PrashantMishra,for
does not work likewhile
.for x in (..,..)
will take values from(.., ..)
one item at a time in each iteration. Try printing:for x in (1,2): print(x)
. What you didfor .. not in (.., ..)
is syntactically wrong.
– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
add a comment |
You can add a try catch block and throw an exception when the entered string does not match your desired value. I have updated your code for the same. Below is the snippet :
def askplayerchoice():
playerchoice = ''
while playerchoice.lower() not in ('X' , 'O'):
try:
playerchoice = str(input('Enter X or O: '))
if playerchoice.lower() not in ['x','o']:
raise ValueError('A very specific bad thing happened.')
if playerchoice.lower() == 'x':
return ['X' , 'O']
else:
return ['O', 'X']
except ValueError:
playerchoice = ''
askplayerchoice()
add a comment |
Your function is returning a value within the while loop, regardless of what the user input is, and that's causing the loop to end. You need to change your else
condition to else if
:
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
add a comment |
First you were initializing playerchoice = ''
but inside the loop you were only checking for playerchoice == 'X'
. But you didn't check for playerchoice == 'O'
. therefore it went to the else and got out of the loop.
This should do the work:
def askplayerchoice():
playerchoice = input('Enter X or O: ')
while playerchoice not in('X', 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X', 'O']
elif playerchoice == 'O':
return ['O', 'X']
askplayerchoice()
add a comment |
When you use return, the while loop will be stopped.
Try this code, it will keep asking.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
print('X' , 'O')
else:
print('O', 'X')
return askplayerchoice()
askplayerchoice()
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%2f53993274%2fthe-while-loop-is-not-repeating-itself-upon-throwing-garbage-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are returning from the loop to the function caller in the first pass itself, because you use return
inside loop. Hence, whatever is the value other than 'X'
, ['O', 'X']
is returned.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You can also make use of an infinite loop while True:
here, like so:
def askplayerchoice():
playerchoice = ''
while True:
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
@PrashantMishra,for
does not work likewhile
.for x in (..,..)
will take values from(.., ..)
one item at a time in each iteration. Try printing:for x in (1,2): print(x)
. What you didfor .. not in (.., ..)
is syntactically wrong.
– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
add a comment |
You are returning from the loop to the function caller in the first pass itself, because you use return
inside loop. Hence, whatever is the value other than 'X'
, ['O', 'X']
is returned.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You can also make use of an infinite loop while True:
here, like so:
def askplayerchoice():
playerchoice = ''
while True:
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
@PrashantMishra,for
does not work likewhile
.for x in (..,..)
will take values from(.., ..)
one item at a time in each iteration. Try printing:for x in (1,2): print(x)
. What you didfor .. not in (.., ..)
is syntactically wrong.
– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
add a comment |
You are returning from the loop to the function caller in the first pass itself, because you use return
inside loop. Hence, whatever is the value other than 'X'
, ['O', 'X']
is returned.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You can also make use of an infinite loop while True:
here, like so:
def askplayerchoice():
playerchoice = ''
while True:
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You are returning from the loop to the function caller in the first pass itself, because you use return
inside loop. Hence, whatever is the value other than 'X'
, ['O', 'X']
is returned.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
You can also make use of an infinite loop while True:
here, like so:
def askplayerchoice():
playerchoice = ''
while True:
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
print(askplayerchoice())
edited Jan 1 at 7:05
answered Jan 1 at 5:50


AustinAustin
11.3k3829
11.3k3829
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
@PrashantMishra,for
does not work likewhile
.for x in (..,..)
will take values from(.., ..)
one item at a time in each iteration. Try printing:for x in (1,2): print(x)
. What you didfor .. not in (.., ..)
is syntactically wrong.
– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
add a comment |
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
@PrashantMishra,for
does not work likewhile
.for x in (..,..)
will take values from(.., ..)
one item at a time in each iteration. Try printing:for x in (1,2): print(x)
. What you didfor .. not in (.., ..)
is syntactically wrong.
– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
Thanks... Worked for while loop.Can you also tell me why this code is not working with For loop.. def askplayerchoice(): playerchoice = '' for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ') if playerchoice == 'X': return ['X' , 'O'] else: return ['O', 'X'] askplayerchoice()
– Prashant Mishra
Jan 1 at 6:57
1
1
@PrashantMishra,
for
does not work like while
. for x in (..,..)
will take values from (.., ..)
one item at a time in each iteration. Try printing: for x in (1,2): print(x)
. What you did for .. not in (.., ..)
is syntactically wrong.– Austin
Jan 1 at 7:01
@PrashantMishra,
for
does not work like while
. for x in (..,..)
will take values from (.., ..)
one item at a time in each iteration. Try printing: for x in (1,2): print(x)
. What you did for .. not in (.., ..)
is syntactically wrong.– Austin
Jan 1 at 7:01
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
yes, you are right.. I got it now !! BIG THANK YOU for clearing the confusion. Happy New year 2019 to you ! :)
– Prashant Mishra
Jan 1 at 7:04
add a comment |
You can add a try catch block and throw an exception when the entered string does not match your desired value. I have updated your code for the same. Below is the snippet :
def askplayerchoice():
playerchoice = ''
while playerchoice.lower() not in ('X' , 'O'):
try:
playerchoice = str(input('Enter X or O: '))
if playerchoice.lower() not in ['x','o']:
raise ValueError('A very specific bad thing happened.')
if playerchoice.lower() == 'x':
return ['X' , 'O']
else:
return ['O', 'X']
except ValueError:
playerchoice = ''
askplayerchoice()
add a comment |
You can add a try catch block and throw an exception when the entered string does not match your desired value. I have updated your code for the same. Below is the snippet :
def askplayerchoice():
playerchoice = ''
while playerchoice.lower() not in ('X' , 'O'):
try:
playerchoice = str(input('Enter X or O: '))
if playerchoice.lower() not in ['x','o']:
raise ValueError('A very specific bad thing happened.')
if playerchoice.lower() == 'x':
return ['X' , 'O']
else:
return ['O', 'X']
except ValueError:
playerchoice = ''
askplayerchoice()
add a comment |
You can add a try catch block and throw an exception when the entered string does not match your desired value. I have updated your code for the same. Below is the snippet :
def askplayerchoice():
playerchoice = ''
while playerchoice.lower() not in ('X' , 'O'):
try:
playerchoice = str(input('Enter X or O: '))
if playerchoice.lower() not in ['x','o']:
raise ValueError('A very specific bad thing happened.')
if playerchoice.lower() == 'x':
return ['X' , 'O']
else:
return ['O', 'X']
except ValueError:
playerchoice = ''
askplayerchoice()
You can add a try catch block and throw an exception when the entered string does not match your desired value. I have updated your code for the same. Below is the snippet :
def askplayerchoice():
playerchoice = ''
while playerchoice.lower() not in ('X' , 'O'):
try:
playerchoice = str(input('Enter X or O: '))
if playerchoice.lower() not in ['x','o']:
raise ValueError('A very specific bad thing happened.')
if playerchoice.lower() == 'x':
return ['X' , 'O']
else:
return ['O', 'X']
except ValueError:
playerchoice = ''
askplayerchoice()
answered Jan 1 at 7:09
Chintan BhattChintan Bhatt
212
212
add a comment |
add a comment |
Your function is returning a value within the while loop, regardless of what the user input is, and that's causing the loop to end. You need to change your else
condition to else if
:
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
add a comment |
Your function is returning a value within the while loop, regardless of what the user input is, and that's causing the loop to end. You need to change your else
condition to else if
:
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
add a comment |
Your function is returning a value within the while loop, regardless of what the user input is, and that's causing the loop to end. You need to change your else
condition to else if
:
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
Your function is returning a value within the while loop, regardless of what the user input is, and that's causing the loop to end. You need to change your else
condition to else if
:
if playerchoice == 'X':
return ['X' , 'O']
elif playerchoice == 'O':
return ['O', 'X']
answered Jan 1 at 5:52


kavehkaveh
7331222
7331222
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
add a comment |
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
Thanks.. What is wrong in this condition: for playerchoice not in ('X' , 'O'): playerchoice = input('Enter X or O: ')
– Prashant Mishra
Jan 1 at 7:02
add a comment |
First you were initializing playerchoice = ''
but inside the loop you were only checking for playerchoice == 'X'
. But you didn't check for playerchoice == 'O'
. therefore it went to the else and got out of the loop.
This should do the work:
def askplayerchoice():
playerchoice = input('Enter X or O: ')
while playerchoice not in('X', 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X', 'O']
elif playerchoice == 'O':
return ['O', 'X']
askplayerchoice()
add a comment |
First you were initializing playerchoice = ''
but inside the loop you were only checking for playerchoice == 'X'
. But you didn't check for playerchoice == 'O'
. therefore it went to the else and got out of the loop.
This should do the work:
def askplayerchoice():
playerchoice = input('Enter X or O: ')
while playerchoice not in('X', 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X', 'O']
elif playerchoice == 'O':
return ['O', 'X']
askplayerchoice()
add a comment |
First you were initializing playerchoice = ''
but inside the loop you were only checking for playerchoice == 'X'
. But you didn't check for playerchoice == 'O'
. therefore it went to the else and got out of the loop.
This should do the work:
def askplayerchoice():
playerchoice = input('Enter X or O: ')
while playerchoice not in('X', 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X', 'O']
elif playerchoice == 'O':
return ['O', 'X']
askplayerchoice()
First you were initializing playerchoice = ''
but inside the loop you were only checking for playerchoice == 'X'
. But you didn't check for playerchoice == 'O'
. therefore it went to the else and got out of the loop.
This should do the work:
def askplayerchoice():
playerchoice = input('Enter X or O: ')
while playerchoice not in('X', 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
return ['X', 'O']
elif playerchoice == 'O':
return ['O', 'X']
askplayerchoice()
edited Jan 1 at 6:01
answered Jan 1 at 5:54


Partho63Partho63
1,8391823
1,8391823
add a comment |
add a comment |
When you use return, the while loop will be stopped.
Try this code, it will keep asking.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
print('X' , 'O')
else:
print('O', 'X')
return askplayerchoice()
askplayerchoice()
add a comment |
When you use return, the while loop will be stopped.
Try this code, it will keep asking.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
print('X' , 'O')
else:
print('O', 'X')
return askplayerchoice()
askplayerchoice()
add a comment |
When you use return, the while loop will be stopped.
Try this code, it will keep asking.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
print('X' , 'O')
else:
print('O', 'X')
return askplayerchoice()
askplayerchoice()
When you use return, the while loop will be stopped.
Try this code, it will keep asking.
def askplayerchoice():
playerchoice = ''
while playerchoice not in ('X' , 'O'):
playerchoice = input('Enter X or O: ')
if playerchoice == 'X':
print('X' , 'O')
else:
print('O', 'X')
return askplayerchoice()
askplayerchoice()
answered Jan 1 at 6:03


WEN WENWEN WEN
9828
9828
add a comment |
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%2f53993274%2fthe-while-loop-is-not-repeating-itself-upon-throwing-garbage-values%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