Python Forum
Using user input to restart and quit a game.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using user input to restart and quit a game.
#1
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!')
Reply
#2
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()
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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:
Reply
#5
(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'):
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,281 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,367 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,967 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,304 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,800 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,136 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,024 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,494 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,559 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 10,737 Mar-02-2019, 10:11 PM
Last Post: johneven

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020