-2

im a newbie to python and my program keeps on closing when typing in the correct decision "y" here is my code feel free to edit my code:im new to python and not the best at using loops.My program should give the user a closing message if "n" or "N" is typed eg.press enter to exit the program and if "yes" or "y" is typed it should carry on going to ask the users name Any Help is very much appreciated:Is my Loop working properly?

play_user = input ("Do You Want To Play?")
play_user = "y" and "Y"
while play_user == "n" and "N":
     play_user = input ("Do You Want To Play")
Fishhy
  • 35
  • 1
  • 7

2 Answers2

0

Try this instead:

while input ("Do You Want To play?").tolower[0:1] != "n":
     play_game()

or

while input ("Do You Want To play?").tolower[0:1] == "y":
     play_game()
wallyk
  • 56,922
  • 16
  • 83
  • 148
0

A couple things first.

Instead of checking if something is equal to its upper case and lower case forms, just use the .lower() method for strings (converts string to lowercase).

But more importantly, what you are doing is setting play_user to something that's not n or N right before the while loop, so it never even enters the while loop.

I would rewrite as

play_user = raw_input("Do you want to play?\n") # note the new line here
while play_user.lower() != "y":
    play_user = raw_input("Do you want to play?\n")

which will keep looping until you enter y or Y.

EchoAce
  • 89
  • 4