How do I make a for loop for my guessing game?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxHana
    New Member
    • Feb 2013
    • 2

    How do I make a for loop for my guessing game?

    Hi, I have a problem with implementing a for loop for my program. I don't understand how to correctly make a for loop in asking the player to play again after getting the number right. The options should be yes as 1 and no as 2.

    I have this so far...:
    Code:
    def playgame():
        import random
        num = random.randint(1,10)
        guess = 0
        score = 0
        name= input(('Hi, what is your name?'))
        print('Hi', name,'. Try to guess my number from 1-10!')
        while(guess != num):
                guess = int(input('GUESS MY NUMBER!'))
                if(num < guess):
                    print('You guessed..', guess)
                    print('THE NUMBER IS TOO HIGH!')
                    score = score+1
                    print ('Your score so far is..', score)
                if(num > guess):
                    print('You guessed..', guess)
                    print('THE NUMBER IS TOO LOW!')
                    score = score+1
                    print ('Your score so far is..', score)
                if(num == guess):
                    print('You guessed..', guess)
                    print('YOU ARE CORRECT!')
                    print('Your final score is..', score)
    
    playgame()
    Last edited by bvdet; Feb 2 '13, 08:13 PM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You should make your import statements at the very top of the code listing. I have not tested it, but something like the following might work for you.
    Code:
    import random
    
    def playgame():
        score = 0
        while True:
            num = random.randint(1,10)
            guess = 0
            name= input(('Hi, what is your name?'))
            print('Hi', name,'. Try to guess my number from 1-10!')
            while(guess != num):
                guess = int(input('GUESS MY NUMBER!'))
                if(num < guess):
                    print('You guessed..', guess)
                    print('THE NUMBER IS TOO HIGH!')
                    score = score+1
                    print ('Your score so far is..', score)
                if(num > guess):
                    print('You guessed..', guess)
                    print('THE NUMBER IS TOO LOW!')
                    score = score+1
                    print ('Your score so far is..', score)
                if(num == guess):
                    print('You guessed..', guess)
                    print('YOU ARE CORRECT!')
                    print('Your final score is..', score)
            if int(input('(1)Play again, (2)Exit')): == 2
                return
     
    playgame()

    Comment

    • xxHana
      New Member
      • Feb 2013
      • 2

      #3
      Thank you for responding! There was some indentation errors I fixed, but now line 26 has a syntax error..

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        It looks like I left off the colon (:).

        Comment

        Working...