Basic question from pure beginner

Discussion in 'Python' started by sato.photo@gmail.com, Jul 1, 2009.

  1. Guest

    I have been going through some Python Programming exercises while
    following the MIT OpenCourseWare Intro to CS syllabus and am having
    some trouble with the first "If" exercise listed on this page:

    http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises

    I have been able to make the module quit after entering a password
    three times, but can't get it to quit right away after the correct one
    is entered. I know this is really basic, please forgive me. I have
    no programming experience and have just started going through these
    tutorials.

    My code is here:

    http://python.pastebin.com/m6036b52e

    -daniel
     
    , Jul 1, 2009
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > I have been going through some Python Programming exercises while
    > following the MIT OpenCourseWare Intro to CS syllabus and am having
    > some trouble with the first "If" exercise listed on this page:
    >
    >

    http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises
    >
    > I have been able to make the module quit after entering a password
    > three times, but can't get it to quit right away after the correct one
    > is entered. I know this is really basic, please forgive me. I have
    > no programming experience and have just started going through these
    > tutorials.
    >
    > My code is here:
    >
    > http://python.pastebin.com/m6036b52e
    >
    > -daniel


    Some hints:

    (1) Have the while loop check for both the correct password and the number
    of tries. Pseudocode:

    while <more tries allowed> and <password not entered correctly>:
    ...

    (2) After the loop has terminated you can check whether the password was
    entered correctly and print the appropriate message.

    Aside: you can't mix if...else with other statements

    correct:

    if cond:
    print "sunny"
    else:
    print "cloudy"
    print "weather"

    wrong:

    if cond:
    print "norwegian"
    print "blue"
    else: # syntax error, else must immediately follow the indented if-block:
    print "moon"

    you could fix it like so:

    if cond:
    print "norwegian"
    print "blue"
    if not cond:
    print "moon"

    Peter
     
    Peter Otten, Jul 1, 2009
    #2
    1. Advertising

  3. alex23 Guest

    On Jul 1, 3:38 pm, "" <>
    wrote:
    > I have been able to make the module quit after entering a password
    > three times, but can't get it to quit right away after the correct one
    > is entered.  


    Not with the code you pasted, you haven't. There's a missing colon on
    line 7 & line 9 isn't indented properly. It's always best if we're
    referring to the same source when trying to help with a problem.

    The problem you're having, though, has to do with the while loop and
    the fact that it's only checking one condition: has the 'count'
    counter been incremented to 3. What you need to do is _also_ check for
    the success condition:

    is_confirmed = False
    while count != 3 or is_confirmed:
    guess = raw_input("Enter your password: ")
    guess = str(guess)
    if guess != password:
    print "Access Denied"
    count = count + 1
    else:
    print "Password Confirmed"
    is_confirmed = True

    This also provides you with a nice boolean that shows whether or not
    the password was confirmed, which may be useful for latter code.

    However, whenever you want to loop a set number of times, it's usually
    better to use a 'for' loop instead:

    PASSWORD = 'qwerty'
    MAXRETRY = 3
    for attempt in xrange(MAXRETRY):
    guess = str(raw_input('Enter your password: '))
    is_complete = guess == PASSWORD
    if is_complete:
    print 'Password confirmed'
    break # this exits the for loop
    else:
    print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)

    This saves you from the burden of creating, incrementing & testing
    your own counter.

    If there's anything here that isn't clear, please don't hesitate to
    ask.
     
    alex23, Jul 1, 2009
    #3
  4. Guest

    Thank you for all of the help. With your assistance and help from the
    Python Tutor mailing list I was able to come up with the following
    code:

    password = "qwerty"
    correct_password_given = False
    guess = "0"
    count = 0
    while count != 3 and not correct_password_given :
    guess = raw_input("Enter your password: ")
    guess = str(guess)
    if guess != password:
    print "Access Denied"
    count = count + 1
    else:
    print "Password Confirmed"
    correct_password_given = True


    If I understand it correctly, the function will continue to loop until
    either the count == 3 or until correct_password_give = True,
    satisfying the two conditions of the assignment, which were to lock a
    user out after three failed password attempts and to print "Access
    Granted" and end the module if the correct password is given. Am I
    understanding this correctly?

    Thanks!

    On Jul 1, 12:06 am, alex23 <> wrote:
    > On Jul 1, 3:38 pm, "" <>
    > wrote:
    >
    > > I have been able to make the module quit after entering a password
    > > three times, but can't get it to quit right away after the correct one
    > > is entered.  

    >
    > Not with the code you pasted, you haven't. There's a missing colon on
    > line 7 & line 9 isn't indented properly. It's always best if we're
    > referring to the same source when trying to help with a problem.
    >
    > The problem you're having, though, has to do with the while loop and
    > the fact that it's only checking one condition: has the 'count'
    > counter been incremented to 3. What you need to do is _also_ check for
    > the success condition:
    >
    > is_confirmed = False
    > while count != 3 or is_confirmed:
    >   guess = raw_input("Enter your password: ")
    >   guess = str(guess)
    >   if guess != password:
    >     print "Access Denied"
    >     count = count + 1
    >   else:
    >     print "Password Confirmed"
    >     is_confirmed = True
    >
    > This also provides you with a nice boolean that shows whether or not
    > the password was confirmed, which may be useful for latter code.
    >
    > However, whenever you want to loop a set number of times, it's usually
    > better to use a 'for' loop instead:
    >
    > PASSWORD = 'qwerty'
    > MAXRETRY = 3
    > for attempt in xrange(MAXRETRY):
    >   guess = str(raw_input('Enter your password: '))
    >   is_complete = guess == PASSWORD
    >   if is_complete:
    >     print 'Password confirmed'
    >     break # this exits the for loop
    >   else:
    >     print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
    >
    > This saves you from the burden of creating, incrementing & testing
    > your own counter.
    >
    > If there's anything here that isn't clear, please don't hesitate to
    > ask.
     
    , Jul 1, 2009
    #4
  5. a écrit :
    > Thank you for all of the help. With your assistance and help from the
    > Python Tutor mailing list I was able to come up with the following
    > code:
    >
    > password = "qwerty"
    > correct_password_given = False
    > guess = "0"


    You could just use None here:

    guess=None


    > count = 0
    > while count != 3 and not correct_password_given :
    > guess = raw_input("Enter your password: ")
    > guess = str(guess)


    IIRC, raw_input() already returns a string !-)

    > if guess != password:
    > print "Access Denied"
    > count = count + 1


    Or:
    count += 1

    > else:
    > print "Password Confirmed"
    > correct_password_given = True
    >
    >
    > If I understand it correctly, the function will continue to loop until
    > either the count == 3 or until correct_password_give = True,


    There are many ways to write a same boolean test, and some are easier to
    understand. Your test could be written:

    while not (count == 3 or correct_password_given) :
    # proceed

    Does it answer your question ?-)

    As a general rule:

    * not A and not B <=> not (A or B)
    * not A or not B <=> not (A and B)


    > satisfying the two conditions of the assignment, which were to lock a
    > user out after three failed password attempts and to print "Access
    > Granted" and end the module if the correct password is given.


    Well, given your snippet, execution indeed terminates after either
    successful 'login' or 3 failed attempts, but that's mostly because
    there's no code to execute after the while loop...
     
    Bruno Desthuilliers, Jul 1, 2009
    #5
  6. MRAB Guest

    wrote:
    > I have been going through some Python Programming exercises while
    > following the MIT OpenCourseWare Intro to CS syllabus and am having
    > some trouble with the first "If" exercise listed on this page:
    >
    > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises
    >
    > I have been able to make the module quit after entering a password
    > three times, but can't get it to quit right away after the correct one
    > is entered. I know this is really basic, please forgive me. I have
    > no programming experience and have just started going through these
    > tutorials.
    >
    > My code is here:
    >
    > http://python.pastebin.com/m6036b52e
    >

    raw_input() returns a string, so you don't need "guess = str(guess)".

    The 'if' line should end with a colon.

    Incrementing the count should probably be after the 'if' statement.

    You can leave a loop prematurely with the 'break' statement.
     
    MRAB, Jul 1, 2009
    #6
  7. Let me offer a bit of editing.

    First, using the condition count != 3 is perhaps risky. A mistake or
    a change in logic in the loop body might result in an infinite loop.
    So instead I suggest

    while count < 3...

    Second, I'd suggest storing the value 3 in a variable with a name that
    describes it.

    MaxAttempts = 3
    while count < MaxAttempts

    This suggests that you change the name 'count' to 'attemptcount'.

    The variable 'guess' is used only inside the loop, so I suggest
    removing the assignment outside the loop.

    Finally, I'd remove correct_password_given from the loop test, and
    replace it with a break statement when the correct password is entered.

    password = "qwerty"
    correct_password_given = False
    attemptcount = 0
    MaxAttempts = 3
    while attemptcount < MaxAttempts:
    guess = raw_input("Enter your password: ")
    guess = str(guess)
    if guess != password:
    print "Access Denied"
    attemptcount = attemptcount + 1
    else:
    print "Password Confirmed"
    correct_password_given = True
    break


    Charles Yeomans

    On Jul 1, 2009, at 3:58 AM, wrote:

    > Thank you for all of the help. With your assistance and help from the
    > Python Tutor mailing list I was able to come up with the following
    > code:
    >
    > password = "qwerty"
    > correct_password_given = False
    > guess = "0"
    > count = 0
    > while count != 3 and not correct_password_given :
    > guess = raw_input("Enter your password: ")
    > guess = str(guess)
    > if guess != password:
    > print "Access Denied"
    > count = count + 1
    > else:
    > print "Password Confirmed"
    > correct_password_given = True
    >
    >
    > If I understand it correctly, the function will continue to loop until
    > either the count == 3 or until correct_password_give = True,
    > satisfying the two conditions of the assignment, which were to lock a
    > user out after three failed password attempts and to print "Access
    > Granted" and end the module if the correct password is given. Am I
    > understanding this correctly?
    >
    > Thanks!
    >
    > On Jul 1, 12:06 am, alex23 <> wrote:
    >> On Jul 1, 3:38 pm, "" <>
    >> wrote:
    >>
    >>> I have been able to make the module quit after entering a password
    >>> three times, but can't get it to quit right away after the correct
    >>> one
    >>> is entered.

    >>
    >> Not with the code you pasted, you haven't. There's a missing colon on
    >> line 7 & line 9 isn't indented properly. It's always best if we're
    >> referring to the same source when trying to help with a problem.
    >>
    >> The problem you're having, though, has to do with the while loop and
    >> the fact that it's only checking one condition: has the 'count'
    >> counter been incremented to 3. What you need to do is _also_ check
    >> for
    >> the success condition:
    >>
    >> is_confirmed = False
    >> while count != 3 or is_confirmed:
    >> guess = raw_input("Enter your password: ")
    >> guess = str(guess)
    >> if guess != password:
    >> print "Access Denied"
    >> count = count + 1
    >> else:
    >> print "Password Confirmed"
    >> is_confirmed = True
    >>
    >> This also provides you with a nice boolean that shows whether or not
    >> the password was confirmed, which may be useful for latter code.
    >>
    >> However, whenever you want to loop a set number of times, it's
    >> usually
    >> better to use a 'for' loop instead:
    >>
    >> PASSWORD = 'qwerty'
    >> MAXRETRY = 3
    >> for attempt in xrange(MAXRETRY):
    >> guess = str(raw_input('Enter your password: '))
    >> is_complete = guess == PASSWORD
    >> if is_complete:
    >> print 'Password confirmed'
    >> break # this exits the for loop
    >> else:
    >> print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
    >>
    >> This saves you from the burden of creating, incrementing & testing
    >> your own counter.
    >>
    >> If there's anything here that isn't clear, please don't hesitate to
    >> ask.

    >
    > --
    >
     
    Charles Yeomans, Jul 1, 2009
    #7
  8. Charles Yeomans a écrit :

    <ot>Please don't top-post (not corrected)</ot>

    > Let me offer a bit of editing.
    >
    > First, using the condition count != 3 is perhaps risky. A mistake or a
    > change in logic in the loop body might result in an infinite loop. So
    > instead I suggest
    >
    > while count < 3...
    >
    > Second, I'd suggest storing the value 3 in a variable with a name that
    > describes it.


    Pretty sensible suggestion, but then:

    > MaxAttempts = 3


    follow conventions instead :

    MAX_ATTEMPS = 3


    (snip)

    > Finally, I'd remove correct_password_given from the loop test, and
    > replace it with a break statement when the correct password is entered.


    Then using a while loop and manually incrementing a counter is a waste
    of time - using a for loop would be simplier and less error-prone.
     
    Bruno Desthuilliers, Jul 1, 2009
    #8
  9. Scott David Daniels a écrit :
    (snip)

    > And even simpler:
    > PASSWORD = "qwerty"
    > MAXRETRY = 3
    > for attempt in range(MAXRETRY):
    > if raw_input('Enter your password: ') == PASSWORD:
    > print 'Password confirmed'
    > break # this exits the for loop
    > print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
    > else:
    > # The else for a for statement is not executed for breaks,
    > # So indicates the end of testing without a match
    > raise SystemExit # Or whatever you'd rather do.


    Which is probably the most pythonic implementation.
     
    Bruno Desthuilliers, Jul 1, 2009
    #9
  10. alex23 Guest

    On Jul 2, 3:47 am, Scott David Daniels <> wrote:
    > And even simpler:
    >      PASSWORD = "qwerty"
    >      MAXRETRY = 3
    >      for attempt in range(MAXRETRY):
    >          if raw_input('Enter your password: ') == PASSWORD:
    >              print 'Password confirmed'
    >              break # this exits the for loop
    >          print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
    >      else:
    >          # The else for a for statement is not executed for breaks,
    >          # So indicates the end of testing without a match
    >          raise SystemExit # Or whatever you'd rather do.


    Nice one, I always forget about for-else :)
     
    alex23, Jul 2, 2009
    #10
  11. On Wed, 1 Jul 2009 20:56:28 -0700 (PDT), alex23 <>
    declaimed the following in gmane.comp.python.general:

    > On Jul 2, 3:47 am, Scott David Daniels <> wrote:
    > > And even simpler:
    > >      PASSWORD = "qwerty"
    > >      MAXRETRY = 3
    > >      for attempt in range(MAXRETRY):
    > >          if raw_input('Enter your password: ') == PASSWORD:
    > >              print 'Password confirmed'
    > >              break # this exits the for loop
    > >          print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
    > >      else:
    > >          # The else for a for statement is not executed for breaks,
    > >          # So indicates the end of testing without a match
    > >          raise SystemExit # Or whatever you'd rather do.

    >
    > Nice one, I always forget about for-else :)


    There is also the getpass module to play with!
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Jul 2, 2009
    #11
  12. alex23 Guest

    Dennis Lee Bieber <> wrote:
    >         There is also the getpass module to play with!


    I don't think I've ever seen getpass, so thanks for pointing that out.
    Unfortunately, it wouldn't have helped the OP understand why his
    original code wasn't working ;)
     
    alex23, Jul 2, 2009
    #12
  13. On Wed, 1 Jul 2009 23:09:13 -0700 (PDT), alex23 <>
    declaimed the following in gmane.comp.python.general:

    > I don't think I've ever seen getpass, so thanks for pointing that out.
    > Unfortunately, it wouldn't have helped the OP understand why his
    > original code wasn't working ;)


    Concur... But I was too lazy to work up an example incorporating
    getpass along with an encrypted password <G>
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Jul 2, 2009
    #13
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    596
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Chele Sedai

    Very basic beginner question (I hope)

    Chele Sedai, Mar 14, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    374
    Chele Sedai
    Mar 14, 2005
  3. peon1000002

    beginner basic question..jdbc

    peon1000002, Feb 16, 2005, in forum: Java
    Replies:
    5
    Views:
    438
    steve
    Feb 17, 2005
  4. Todd Aspeotis
    Replies:
    3
    Views:
    470
    Kanenas
    May 30, 2005
  5. Replies:
    4
    Views:
    799
    Ben C
    Mar 29, 2008
Loading...

Share This Page