Python Forum

Full Version: Using user input to restart and quit a game.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I can't figure out how to use user input, to restart and quit the game.


    import random

    guessesTaken = 0


    print('Hello! What is your name?')
    myName = input()

    number = random.randint(1, 10)
    print('Well, ' + myName + ', I am thinking of a number between 1 and 10.')

    guess = input

    while guess != number:
        print('Take a guess.') 
        guess = input()
        guess = int(guess)

        guessesTaken = guessesTaken + 1
                                            
        if guess < number:
            print('Your guess is too low.') 

        if guess > number:
            print('Your guess is too high.')

        if guess == number:
            break

        if guess == number:
            guessesTaken = str(guessesTaken)
            print('Good job, ' + myName + '! You guessed my number in ' +
            guessesTaken +  ' guesses!')
I figured it out by using a main function.

def main():

    import random

    guessesTaken = 0

    print('Hello! What is your name?')
    myName = input()

    number = random.randint(1, 10)
    print('Well, ' + myName + ', I am thinking of a number between 1 and 10.')

    guess = input

    while guess != number:
          print('Take a guess.') 
          guess = input()
          guess = int(guess)
          guessesTaken = guessesTaken + 1                                 

          if guess < number:
              print('Your guess is too low.') 

          if guess > number:
              print('Your guess is too high.')


          if guess == number:
              guessesTaken = str(guessesTaken)
              restart=input('Good job, '+ myName + '! You guessed my number in '
              + guessesTaken + ' guesses! Type yes to play again and no to quit:').lower()
              if restart == 'yes':
                  main()

              else:
                  exit

main()
For games, I usually call the 'main' function 'play'. Then I might do something like:

if __name__ == '__main__':
    while True:
        play()
        again = input('Would you like to play again? ')
        if again.lower in ('yes', 'y'):
            break
If any of that code doesn't make sense to you, let me know and I can explain it for you.
I'm glad you figured out a solution, but please do not loop using recursion for tasks such as this. Although in this particular case you are unlikely to loop enough times to blow out the stack, it is a bad habit to start thinking you can do things this way.

For example the following extremely reduced version of your code:
def main():
    main()


main()
Produces the following error:
Error:
Traceback (most recent call last): File "blargle.py", line 5, in <module> main() File "blargle.py", line 2, in main main() File "blargle.py", line 2, in main main() File "blargle.py", line 2, in main main() [Previous line repeated 995 more times] RecursionError: maximum recursion depth exceeded
Recursion is a technique that should be used when the problem size for each subsequent call is smaller than the last, finally terminating in a base case.

What you need is just a while loop.

Here is an example, retaining the majority of your code:
(Sep-15-2018, 08:00 PM)ichabod801 Wrote: [ -> ]For games, I usually call the 'main' function 'play'. Then I might do something like:

if __name__ == '__main__':
    while True:
        play()
        again = input('Would you like to play again? ')
        if again.lower in ('yes', 'y'):
            break
If any of that code doesn't make sense to you, let me know and I can explain it for you.
sorry @ichabod801, you missed some brackets:
needs to be if again.lower() in ('yes', 'y'):