1

I am attempting to make my program repeat when the user inputs y/n, however I am confused on how to use a while true with this type of input, below is some code.

again = input("Would you like to play again? enter y/n:  ")
if again == "n":
    print ("Thanks for Playing!")
    quit

if again == "y":
    print ("Lets play again..")
    ????

Also, I want to do an else statement if the user enters in a different character, but am unsure on how to go about that considering I have 2 different if statements.

Ryan Tice
  • 731
  • 3
  • 13
  • 16

4 Answers4

5

When you're writing a standalone Python program, it’s a good practice to use a main function. it allows you to easily add some unit tests, use your functions or classes from other modules (if you import them), etc.

If you have to check if some condition is satisfied in case some other condition is not satisfied, and perform some actions depending on which condition is true, you can use an if…elif…else statement.

Also, please note that you cannot use the input() function for your program in this case. What you really want to use here is raw_input. The difference between these two functions is that raw_input() will always return a string and input() will evaluate user’s input as if it was written in your code instead of input(). So, if the user enters "y" (with the quotation marks), then a string object is stored as the value for the variable. But if the user enters y (without the quotation marks), input() will try to evaluate this and an error will be thrown if y is not defined.

You can read more on this subject here.

def main():
    while True:
        again = raw_input("Would you like to play again? Enter y/n: ")

        if again == "n":
            print ("Thanks for Playing!")
            return
        elif again == "y":
            print ("Lets play again..")
        else:
            print ("You should enter either \"y\" or \"n\".")

if __name__ == "__main__":
    main()
Arseny
  • 5,159
  • 4
  • 21
  • 24
  • That does what I need, except for starting at the beginning of the program, I did not post the early code though, would I place the entire code in the main function? – Ryan Tice Sep 24 '12 at 00:40
  • Yep, threw my original code into the loop and it worked as expected, thanks for the help. – Ryan Tice Sep 24 '12 at 00:46
  • Usually you use the file to define different function, including the main() function for the files you are going to execute directly. So you use the main() function for everything that should be executed when the user runs the program (but if it uses some other functions, you can, of course, put them outside of the main() fuction or even outside of this particular file). – Arseny Sep 24 '12 at 00:47
1
def play_game():
    if int(raw_input("Guess a number:"))!= 5:
          print "You Lose!"
    else:
          print "You Win!"

def play_again():
    return raw_input("Play Again?").lower() == "y"

while True:
    play_game()
    if not play_again(): break

print "OK Goodbye..."
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

You could do something like this:

Assign a bool value to a variable called playing, and then use that as the loop condition.

So you would have;

playing = True
while playing:
    choice = input("would you like to play again? y/n: ")
    if choice == "n":
        print "Thanks for playing"
        playing = False
    else:
        print "play again.. etc..."

Setting the playing variable to false with cause the loop to terminate.

Audionautics
  • 530
  • 4
  • 12
0

I got my code to work and it's looping everytime it goes to else statement, basically loops back to the if statement..

Just started to learn python and I'm really liking it. here is my simple code.

print 'Welcome to "Guess my number"'

def main():
    while True:
        number = raw_input('Please Enter a number between 1 and 10: ')
        if number == '5':
            print 'You Got It!! It\'s number ' + number
            return
        else:
            print 'Please try again!'
main()
raw_input("\nPress enter")
ReelCode
  • 11
  • 1