Guess the Number Repeat

Discussion in 'Python' started by eschneider92@comcast.net, Apr 25, 2013.

  1. Guest

    How do I make the following program play the 'guess the number game' twice?

    import random
    guessesTaken = 0
    print('Hello! What is your name?')
    myName = input()
    number = random.randint(1, 20)
    print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
    while guessesTaken < 6:
    print('Take a guess.')
    guess = input()
    guess = int(guess)
    print('You have ' + str(5 - guessesTaken) + ' guesses left.')
    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!')
    if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)
     
    , Apr 25, 2013
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > How do I make the following program play the 'guess the number game'
    > twice?
    >
    > import random
    > guessesTaken = 0
    > print('Hello! What is your name?')
    > myName = input()
    > number = random.randint(1, 20)
    > print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
    > while guessesTaken < 6:
    > print('Take a guess.')
    > guess = input()
    > guess = int(guess)
    > print('You have ' + str(5 - guessesTaken) + ' guesses left.')
    > 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!')
    > if guess != number:
    > number = str(number)
    > print('Nope. The number I was thinking of was ' + number)



    If you put everything needed to guess once into a function like in the
    following example. Once you have reorganised

    #first version -- flat
    guess = int(input("guess my number: "))
    if guess == 42:
    print("congrats")
    else:
    print("sorry, you're wrong")

    into

    #second version -- using a function
    def guess_number():
    guess = int(input("guess my number: "))
    if guess == 42:
    print("congrats")
    else:
    print("sorry, you're wrong")

    guess_number()

    you can easily modify the script to invoke the guess_number() function
    inside a loop:

    #third version -- calling the function from within a loop
    def guess_number():
    guess = int(input("guess my number: "))
    if guess == 42:
    print("congrats")
    else:
    print("sorry, you're wrong")

    for i in range(2):
    print("round", i+1)
    guess_number()
     
    Peter Otten, Apr 25, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EAS

    Guess My Number Game

    EAS, May 15, 2004, in forum: Python
    Replies:
    8
    Views:
    950
    Andrew Dalke
    May 16, 2004
  2. Weng Tianxiang
    Replies:
    63
    Views:
    2,113
    Weng Tianxiang
    Sep 26, 2007
  3. Guest
    Replies:
    0
    Views:
    589
    Guest
    Sep 17, 2007
  4. Scott W Dunning

    Help with "Guess the number" script

    Scott W Dunning, Mar 1, 2014, in forum: Python
    Replies:
    0
    Views:
    85
    Scott W Dunning
    Mar 1, 2014
  5. Dennis Lee Bieber

    Re: Help with "Guess the number" script

    Dennis Lee Bieber, Mar 1, 2014, in forum: Python
    Replies:
    7
    Views:
    71
    Dave Angel
    Mar 3, 2014
Loading...

Share This Page