New to programming question

Discussion in 'Python' started by Ben, Apr 1, 2005.

  1. Ben

    Ben Guest

    This is an exercise from the Non-programmers tutorial for Python
    by Josh Cogliati.

    The exercise is:

    Write a program that has a user guess your name, but they only get 3
    chances to do so until the program quits.

    Here is my script:

    --------------------------

    count = 0
    name = raw_input("Guess my name. ")

    while name != 'Ben' and count < 3:
    count = count + 1

    if name != 'Ben' and count < 3:
    name = raw_input('Guess again. ')
    elif name == 'Ben' and count < 3:
    print "You're right!"
    else:
    print 'No more tries.'

    ----------------------------------

    Everything works except the line: print "You're right!"

    Could someone tell me what is wrong and give me a better alternative to
    what I came up with.

    Thank you

    Ben
     
    Ben, Apr 1, 2005
    #1
    1. Advertisements

  2. The code within the while loop (i.e., everything indented) is executed only if
    the while condition is true, i.e., name != Ben and count < 3

    So name == 'Ben': will always be false and "You're right!" will never get printed



    and give me a better alternative to
    Just a hint: you may find a cleaner solution if you separate the tests for name
    from the test for count.

    HTH

    Michael
     
    Michael Spencer, Apr 1, 2005
    #2
    1. Advertisements

  3. Ben

    Ron_Adam Guest

    Seperate you raw input statements from your test. Your elsif is
    skipping over it.

    Try using only one raw imput statement right after your while
    statement.

    Ron
     
    Ron_Adam, Apr 1, 2005
    #3
  4. Ben

    Joal Heagney Guest

    Everything inside this loop will only occur if the name doesn't equal
    'Ben' and the count is less than 3.
    You increase the count by one, which allows your code to catch the case
    where count = 2 and now equals 3.
    Which is why you get this print message, because count is now equal to 3.

    But at no point does the program get an opportunity to print "No more
    tries.' because there is no point inside this loop where name == 'Ben'.
    Also, you're duplicating a lot of your case testing. You check to see if
    the name is 'Ben' at the start, and then inside the loop, and the same
    for the counts.

    I tried to write out a logical method of approaching this problem, but
    in truth this particular use-case isn't that simple is it?

    Here's my contribution anycase:

    count = 0
    # Get first input
    name = raw_input("Guess my name: ")
    # Give the sucker two extra goes
    while count < 2:
    # Check the value of name
    if name == 'Ben':
    print "You're right!"
    break
    else:
    name = raw_input("Try again: ")
    # Of course, we haven't checked the sucker's last guess
    # so we have to do that now.
    if count == 2:
    if name == 'Ben':
    print "You're right!"
    else:
    print "No more tries for you!!!"


    Hope this helps.
    Joal
     
    Joal Heagney, Apr 1, 2005
    #4
  5. Ben

    Joal Heagney Guest

    Oh goddammmnitttt. I seem to be doing this a lot today. Look below for
    the extra addition to the code I posted.

    # Here's the bit I missed out.
    count += 1
    GRRRRRRRR.

    Joal
     
    Joal Heagney, Apr 1, 2005
    #5
  6. Need something more straightforward, e.g., a wrapped one-liner:
    ... (x for x in xrange(n) for t in [raw_input('Guess my name: ')=='Ben']
    ... if not t or iter([]).next())]
    ... Guess my name: Jack
    Guess my name: Bob
    Guess my name: Ben
    You're right! Guess my name: Jack
    Guess my name: Ben
    You're right! Guess my name: Kermit
    Guess my name: Ms Piggy
    Guess my name: Ernie
    No more tries for you!!! Guess my name: Einstein
    No more tries for you!!! Guess my name: Ben
    You're right!

    Regards,
    Bengt Richter
     
    Bengt Richter, Apr 1, 2005
    #6
  7. Ben

    Joal Heagney Guest

    Okay, now in my opinion, that's just too complex to give to a newbie as
    a suggested implementation. :)

    Joal
     
    Joal Heagney, Apr 1, 2005
    #7
  8. Ben

    Steve Holden Guest

    I suppose this would be far too easy to understand, then:

    pr =['Guess my name', 'Wrong, try again', 'Last chance']
    for p in pr:
    name = raw_input(p+": ")
    if name == "Ben":
    print "You're right!"
    break
    else:
    print "Loser: no more tries for you"

    regards
    Steve
     
    Steve Holden, Apr 2, 2005
    #8
  9. Ben

    Ben Guest

    Thanks for your help.

    It is much appreciated.
     
    Ben, Apr 2, 2005
    #9
  10. Ben

    Ben Guest

    Thanks for your input.
     
    Ben, Apr 2, 2005
    #10
  11. Ben

    Ben Guest

    Thanks for your reply.
     
    Ben, Apr 2, 2005
    #11
  12. Ben

    Ben Guest

    Joal was right. It is a bit beyond me. But I appreciate your response.
     
    Ben, Apr 2, 2005
    #12
  13. Ben

    Joal Heagney Guest

    THIS is why I like python! There's always a simple, easy to understand
    way to do something. If it looks complex, then there must me something
    wrong.

    Joal
     
    Joal Heagney, Apr 2, 2005
    #13
  14. Ben

    Joal Heagney Guest

    And now that I've looked at the documentation of the for loop, I
    understand it as well! :)

    The following explaination is for Ben, so he knows what's going on.
    From the documentation, with a little rewriting.


    "The for statement is used to iterate over the elements of a sequence
    (such as a string, tuple or list) or other iterable object:

    for target_list "in" expression_list:
    <do this first>
    else:
    <now do this last>

    The expression list is evaluated once and should yield a sequence(such
    as a string, tuple, list or iterator object).
    Each item in the sequence is assigned to the target_list variable in
    turn. Then the "do this first" instructions are then executed once for
    each item in the sequence.
    When the items are exhausted (which is immediately when the sequence is
    empty), the "now do this last" instructions in the else statement, if
    present, are executed, and the loop terminates.

    A break statement executed in the first section terminates the WHOLE
    loop without executing the else clause. A continue statement executed in
    the first stage skips the rest of these instructions for that loop and
    continues with the next item, or with the else clause if there was no
    next item."

    So copying Steve's example:
    This allows us to execute the else clause if the name is guessed
    incorrectly three times.
    However, if the name is guessed correctly, then the break statement
    pulls us completely out of the loop without executing the else clause.

    My original example attempted to do this by splitting the loop up into a
    series of different cases because I was unaware of this additional
    behaviour with the for loop expression. Steve's method = much better.

    Joal
     
    Joal Heagney, Apr 2, 2005
    #14
  15. Well, it's been two days since your post, and the other
    suggestions should have given you enough to make a working version, so I
    shouldn't be violating the "we don't do homework" ethic by too much with
    this...

    -=-===-=-=-=-=-=-
    myName = "Ben"
    numTries = 3

    print "Guess my name (you get three tries)"
    for t in range(numTries):
    guess = raw_input("Your Guess> ")
    if guess.upper() == myName.upper():
    print "You guessed my name in %s tries" % (t + 1)
    break
    else:
    print "%s is not my name" % guess
    else:
    print "Sorry, you lose"
    -=-=-=-=-=-=-=-=-=-

    G:\My Documents>python t.py
    Guess my name (you get three tries)
    Your Guess> Able
    Able is not my name
    Your Guess> Cain
    Cain is not my name
    Your Guess> Serpent
    Serpent is not my name
    Sorry, you lose

    G:\My Documents>python t.py
    Guess my name (you get three tries)
    Your Guess> Who
    Who is not my name
    Your Guess> is
    is is not my name
    Your Guess> ben
    You guessed my name in 3 tries

    G:\My Documents>

    --
     
    Dennis Lee Bieber, Apr 2, 2005
    #15
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.