python - User input and stuck in while loop -
i want code take user input, prompt user continue, if answer 'y' asks input, if answer 'n' program stops , if type other characters continues prompt them until enter 'y' or 'n'.
as code shows i'm trying use while loop continuously prompt user until enter 'y' or 'n'. when reach while loop not stop when 'y' or 'n' entered.
def test(): number = input('input number press enter:') print(number) prompt = input('continue (y/n)? ') if prompt == 'y': number = input('input number press enter:') print(number) prompt = input('continue (y/n)? ') elif prompt == 'n': pass else: while prompt != 'y' or 'n': prompt = input('continue (y/n)? ')
this not how or
works:
while prompt != 'y' or 'n':
you meant:
while prompt != 'y' or prompt != 'n':
your version or
s prompt != 'y'
, 'n'
, yields @ least last truth-y value ('n'
).
the full code:
def test(): number = input('input number press enter:') print(number) prompt = input('continue (y/n)? ') if prompt == 'y': number = input('input number press enter:') print(number) prompt = input('continue (y/n)? ') elif prompt == 'n': pass else: while prompt != 'y' or prompt != 'n': prompt = input('continue (y/n)? ')
to these kind of input loops use while true
break
:
def test(): prompt = 'y' while true: if prompt == 'y': number = input('input number press enter:') print(number) elif prompt == 'n': break prompt = input('continue (y/n)? ')
Comments
Post a Comment