Basic while loop syntax error (101 stuff)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eppi92
    New Member
    • Aug 2013
    • 2

    Basic while loop syntax error (101 stuff)

    I am getting a syntax error as I try to run my little "Guess the number"-game, as directly copied from http://inventwithpython.com/chapter4.html. The error reads:
    File "guess.py", line 12
    while guessesTaken < 6:
    ^
    SyntaxError: invalid syntax

    The source code reads:
    Code:
    # This is a guess the number game.
    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)
    
        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, ' + myNamen + '! You guessed my number in ' + guessesTaken + ' guesses!')
    
    if guess != number:
          number = str(number)
          print('Nope. The number I was thinking of was ' + number)
    I am running Python 3.3 on Windows 7, and have tried both running the program from IDLE and Command.
    Last edited by bvdet; Aug 25 '13, 11:08 PM. Reason: Add code tags
  • eppi92
    New Member
    • Aug 2013
    • 2

    #2
    Solved it by pasting piece by piece into a new file, testing as I went along. Discovered 1 typo towards the end of the program (line 30, "myNamen" is not a variable), but still don't know what caused the original error. Ghost in the machine, I guess..

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      The problem with the first one is because the print before line 12 never got closed.

      Comment

      Working...