First python program, syntax error in while loop

Discussion in 'Python' started by ryankoch38@gmail.com, May 3, 2013.

  1. Guest

    title = "Guess my number game:"
    print title.title()
    raw_input("Press any key to continue..")

    import random

    tries = 0
    number = random.randrange(99) + 1
    guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")

    while (guess != number):
    if (guess > number):
    number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
    tries += 1
    else if (guess < number):
    number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
    tries += 1
    print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"

    raw_input("\n\n Press any key to exit..")

    ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :p
    , May 3, 2013
    #1
    1. Advertising

  2. On Fri, May 3, 2013 at 7:18 PM, <> wrote:
    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")
    >
    > import random
    >
    > tries = 0
    > number = random.randrange(99) + 1
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
    >
    > while (guess != number):
    > if (guess > number):
    > number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
    > tries += 1
    > else if (guess < number):
    > number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
    > tries += 1
    > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
    >
    > raw_input("\n\n Press any key to exit..")
    >
    > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :p
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    1. post full tracebacks.
    2. The contents of your while loop must be indented, just like the
    contents of the if/else if statement you have there. So, four spaces
    before the 'if', 'else if' and 'print' lines; eight before 'number'
    and 'tries' lines.

    --
    Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
    stop html mail | always bottom-post
    http://asciiribbon.org | http://caliburn.nl/topposting.html
    Chris “Kwpolska†Warrick, May 3, 2013
    #2
    1. Advertising

  3. John Gordon Guest

    In <> writes:

    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")


    > import random


    > tries = 0
    > number = random.randrange(99) + 1
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")


    > while (guess != number):
    > if (guess > number):
    > number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
    > tries += 1
    > else if (guess < number):
    > number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
    > tries += 1
    > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"


    > raw_input("\n\n Press any key to exit..")


    > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :p


    You're missing a second closing parentheses on the line where the user
    inputs their guess.

    Also, you need to indent the 'if' statement underneath the while loop.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2013
    #3
  4. MRAB Guest

    On 03/05/2013 18:18, wrote:
    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")
    >
    > import random
    >
    > tries = 0
    > number = random.randrange(99) + 1
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
    >
    > while (guess != number):
    > if (guess > number):
    > number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
    > tries += 1
    > else if (guess < number):
    > number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
    > tries += 1
    > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
    >
    > raw_input("\n\n Press any key to exit..")
    >
    > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :p
    >

    Indentation in important in Python.

    Also, "else if" should be "elif", and you ask for a guess only once.


    title = "Guess my number game:"
    print title.title()
    raw_input("Press any key to continue..")

    import random

    tries = 0
    number = random.randrange(99) + 1

    # Ask for first guess.
    guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")

    while guess != number:
    if guess > number:
    number = int(raw_input("Sorry, my number is lower than that! \n
    Try again:")
    tries += 1
    elif guess < number:
    number = int(raw_input("Sorry, my number is higher than that!
    \n Try again:")
    tries += 1

    # Ask for next guess.
    guess = int(raw_input("Guess my number! Secret - It is between 1
    and 100 :")

    print "Congratulations, you guessed my number! \n And it only took you"
    tries "tries!"

    raw_input("\n\n Press any key to exit..")
    MRAB, May 3, 2013
    #4
  5. Zachary Ware Guest

    On Fri, May 3, 2013 at 12:18 PM, <> wrote:
    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")
    >
    > import random
    >
    > tries = 0
    > number = random.randrange(99) + 1
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")


    First up, there's a missing ) on each call to int(), which is causing
    the syntax error you see. The error points at "while" because
    "while" doesn't make sense in the call to int().

    >
    > while (guess != number):
    > if (guess > number):


    After fixing the error above, you'll have another one here: you
    haven't indented the block to be executed in the while loop.
    Indentation is important in Python, it delimits code blocks and makes
    things more readable for people.

    > number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
    > tries += 1


    There's also another indentation error here: everything in the same
    block has to be indented to the same level.

    > else if (guess < number):
    > number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
    > tries += 1
    > print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"


    And here will be another syntax error: you have to separate your
    arguments to 'print' with commas.

    >
    > raw_input("\n\n Press any key to exit..")
    >
    > ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :p



    Once you have those errors fixed, I'll bet I can guess your number on
    the second try every time. I'll let you figure out why yourself,
    though ;).
    Zachary Ware, May 3, 2013
    #5
  6. John Gordon Guest

    In <> =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= <> writes:

    > 1. post full tracebacks.


    I almost responded with the same advice, but in this case the full
    traceback doesn't really tell us anything more:

    File "foo.py", line 11
    while guess != number:
    ^
    SyntaxError: invalid syntax

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2013
    #6
  7. Guest

    Okay, thank you very much for the timely replies, heres what i have now:

    title = "Guess my number game:"
    print title.title()
    raw_input("Press any key to continue..")

    import random

    number = random.randrange(99) + 1
    tries = 0
    guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

    while (guess != number):
    if (guess > number):
    number = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
    tries += 1
    else:
    number = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
    tries += 1

    print "good job, you won!"



    raw_input("\n\n Press any key to exit..")


    ## it seems to stick with "higher" or "lower" after my first guess, whichever it
    ## is
    , May 3, 2013
    #7
  8. John Gordon Guest

    In <> writes:

    > Okay, thank you very much for the timely replies, heres what i have now:


    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")


    > import random


    > number = random.randrange(99) + 1
    > tries = 0
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))


    > while (guess != number):
    > if (guess > number):
    > number = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
    > tries += 1
    > else:
    > number = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
    > tries += 1
    >
    > print "good job, you won!"
    >



    > raw_input("\n\n Press any key to exit..")



    > ## it seems to stick with "higher" or "lower" after my first guess, whichever it
    > ## is


    When the user re-enters their guess in the while loop, you're assigning
    the input to "number" instead of "guess".

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2013
    #8
  9. Guest

    Oh wow I can't believed I derped that hard!!!!
    Thanks Lol.
    , May 3, 2013
    #9
  10. Guest

    I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails..

    title = "Guess my number game:"
    print title.title()
    raw_input("Press any key to continue..")

    import random

    number = random.randrange(99) + 1
    tries = 0
    guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

    while (guess != number):
    if (guess > number):
    guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
    tries += 1
    if (guess < number):
    guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
    tries += 1
    if (tries > 5):
    print "Sorry, you lost!"
    break

    print "\nCongratulations! You guessed my number in", tries, "tries"


    raw_input("\n\n Press any key to exit..")

    ## this results in the congratulations printing after the second/third guess and ## continuing
    , May 3, 2013
    #10
  11. John Gordon Guest

    In <> writes:

    > I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails


    The "Congratulations" message is inside the while loop; that's why it
    always prints.

    To break after five guesses, you could change the while loop to this:

    while (guess != number) and (tries < 5):

    And then after the while loop, put this if statement:

    if (tries < 5):
    print "\nCongratulations! You guessed my number in", tries, "tries"

    else:
    print "Sorry, you lost!"

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2013
    #11
  12. Guest

    Thank you! It's 100% functional now, here's the final project:

    title = "Guess my number game:"
    print title.title()
    raw_input("Press any key to continue..")

    import random

    number = random.randrange(99) + 1
    tries = 0
    guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))

    while (guess != number) and (tries <5):
    if (guess > number):
    guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
    tries += 1
    if (guess < number):
    guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
    tries += 1

    if (tries <5):
    print "\nCongratulations! You guessed my number in", tries, "tries"
    else:
    print "\nSorry, you took too many tries to guess my number!"


    raw_input("\n\n Press any key to exit..")

    ## Maybe now I can work on a useful project
    , May 3, 2013
    #12
  13. Neil Cerutti Guest

    On 2013-05-03, <> wrote:
    > Thank you! It's 100% functional now, here's the final project:
    >
    > title = "Guess my number game:"
    > print title.title()
    > raw_input("Press any key to continue..")
    >
    > import random
    >
    > number = random.randrange(99) + 1
    > tries = 0
    > guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
    >
    > while (guess != number) and (tries <5):
    > [...]
    >
    > if (tries <5):
    > print "\nCongratulations! You guessed my number in", tries, "tries"
    > else:
    > print "\nSorry, you took too many tries to guess my number!"
    > raw_input("\n\n Press any key to exit..")
    >
    > ## Maybe now I can work on a useful project


    Not quite yet. Players who guess correctly on the fifth try don't
    get credit.

    --
    Neil Cerutti
    Neil Cerutti, May 3, 2013
    #13
  14. John Gordon Guest

    In <> Neil Cerutti <> writes:

    > Not quite yet. Players who guess correctly on the fifth try don't
    > get credit.


    Are you sure? tries is initialized to zero and isn't incremented for the
    initial guess.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, May 3, 2013
    #14
  15. Neil Cerutti Guest

    On 2013-05-03, John Gordon <> wrote:
    > In <> Neil Cerutti <> writes:
    >
    >> Not quite yet. Players who guess correctly on the fifth try don't
    >> get credit.

    >
    > Are you sure? tries is initialized to zero and isn't
    > incremented for the initial guess.


    while (number != guess) and (tries < 5):

    Is the condition that concludes the game.

    After the game, you are told you lost if tries is not less than
    five, regardless of if number == guess.

    --
    Neil Cerutti
    Neil Cerutti, May 6, 2013
    #15
  16. On 06/05/2013 13:06, Neil Cerutti wrote:
    > On 2013-05-03, John Gordon <> wrote:
    >> In <> Neil Cerutti <> writes:
    >>
    >>> Not quite yet. Players who guess correctly on the fifth try don't
    >>> get credit.

    >>
    >> Are you sure? tries is initialized to zero and isn't
    >> incremented for the initial guess.

    >
    > while (number != guess) and (tries < 5):
    >
    > Is the condition that concludes the game.
    >
    > After the game, you are told you lost if tries is not less than
    > five, regardless of if number == guess.
    >


    One of these days I'll work out why some people insist on using
    superfluous parentheses in Python code. Could it be that they enjoy
    exercising their fingers by reaching for the shift key in conjunction
    with the 9 or 0 key?

    --
    If you're using GoogleCrap™ please read this
    http://wiki.python.org/moin/GoogleGroupsPython.

    Mark Lawrence
    Mark Lawrence, May 6, 2013
    #16
  17. Neil Cerutti Guest

    On 2013-05-06, Mark Lawrence <> wrote:
    > On 06/05/2013 13:06, Neil Cerutti wrote:
    >> On 2013-05-03, John Gordon <> wrote:
    >>> In <> Neil Cerutti <> writes:
    >>>
    >>>> Not quite yet. Players who guess correctly on the fifth try don't
    >>>> get credit.
    >>>
    >>> Are you sure? tries is initialized to zero and isn't
    >>> incremented for the initial guess.

    >>
    >> while (number != guess) and (tries < 5):
    >>
    >> Is the condition that concludes the game.
    >>
    >> After the game, you are told you lost if tries is not less than
    >> five, regardless of if number == guess.

    >
    > One of these days I'll work out why some people insist on using
    > superfluous parentheses in Python code. Could it be that they
    > enjoy exercising their fingers by reaching for the shift key in
    > conjunction with the 9 or 0 key?


    Superflous parenthesis are sometimes a nice aid to comprehension.

    I don't know about the above case, though. I don't think it hurts
    anything, but it doesn't add much.

    --
    Neil Cerutti
    Neil Cerutti, May 6, 2013
    #17
  18. Roy Smith Guest

    In article <>,
    Mark Lawrence <> wrote:

    > > while (number != guess) and (tries < 5):

    >
    > One of these days I'll work out why some people insist on using
    > superfluous parentheses in Python code. Could it be that they enjoy
    > exercising their fingers by reaching for the shift key in conjunction
    > with the 9 or 0 key?


    There's lots of reasons. Some valid, some not.

    Lets dispense with the invalid reason first. They've come from
    C/C++/Java/whatever and are used to typing parens around the conditions
    for if/for/while statements. To them, I say, "Stop trying to write
    FORTRAN code in languages that aren't FORTRAN".

    In this case, however, I have no problem with the extra parens. Look at
    these two statements:

    >> while (number != guess) and (tries < 5):
    >> while number != guess and tries < 5:


    They have the same meaning. To correctly interpret the second one, you
    need to know that != and < bind tighter than "and". One could say that
    you should know that, and maybe you would be right.

    On the other hand, I've long since given up trying to remember operator
    precedence in various languages. If I ever have even the slightest
    doubt, I just go ahead and put in the extra parens. It takes another
    few ms to type, and it removes all ambiguity (for both me, and every
    future person who has to read my code). And, every once in a while, it
    keeps me from writing a subtle and hard-to-find bug because the
    precedence rules I was sure I had remembered correctly turned out to be
    wrong for the language I happened to be typing that day.

    BTW, in C, I used to write:

    return (foo)

    for years until somebody pointed out to me that

    return foo

    works. I just assumed that if I had to write:

    if (foo)
    while (foo)
    for (foo; bar; baz)

    then

    return (foo)

    made sense too.
    Roy Smith, May 6, 2013
    #18
  19. rusi Guest

    On May 6, 6:08 pm, Roy Smith <> wrote:

    > BTW, in C, I used to write:
    >
    > return (foo)
    >
    > for years until somebody pointed out to me that
    >
    > return foo
    >
    > works.  I just assumed that if I had to write:
    >
    > if (foo)
    > while (foo)
    > for (foo; bar; baz)
    >
    > then
    >
    > return (foo)
    >
    > made sense too.


    I guess its because K&R always show their examples with
    return (expr);
    and so we all assume its the propah way
    rusi, May 6, 2013
    #19
  20. On Mon, May 6, 2013 at 11:08 PM, Roy Smith <> wrote:
    > On the other hand, I've long since given up trying to remember operator
    > precedence in various languages. If I ever have even the slightest
    > doubt, I just go ahead and put in the extra parens.


    If I ever have even the slightest doubt, I just go ahead and type
    "<language> operator precedence" into a web search and check it :)
    Aside from utter insanity like PHP's ternary operator being wrongly
    associative, that covers pretty much everything.

    ChrisA
    Chris Angelico, May 6, 2013
    #20
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,218
    Tim Slattery
    Mar 30, 2005
  2. Uday Bidkar
    Replies:
    4
    Views:
    476
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  3. pradeep
    Replies:
    2
    Views:
    475
    Anssi Saari
    Dec 23, 2009
  4. laredotornado

    strange syntax error in while loop

    laredotornado, Jan 23, 2008, in forum: Ruby
    Replies:
    2
    Views:
    119
    Dominik Honnef
    Jan 23, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page