.

Python Help

<<

tab

Newbie
Newbie

Posts: 2

Joined: Mon Aug 17, 2009 8:24 pm

Post Mon Aug 17, 2009 9:10 pm

Python Help

Hello, I'm new to this site. I have started studying programming as a hobby and I'm really getting into it, hopefully I can make a career out of it but if not I like it as a hobby.

Well, I started two days ago and I can't seem to be able to comprehend this little section.

This is the whole program but the part that is under the "---" is what I don't get.
I don't understand why there is a while loop on "logged" and why the "login" is phil, the password "McChicken" and why they are in while loop.

Sorry, for the long post I tried to make it as organized as best as I can.

tab-

  Code:
while 1 == 1:
    nameguess=passwordguess=key=""
    while (nameguess != name) or (passwordguess != password):
        nameguess = raw_input('Name? ')
        passwordguess = raw_input('Password? ')
    print "Welcome,", name, ". Type lock to lock."
    while key != "lock":
        key = raw_input("")
-----------------------------
login = "john"
password = "tucker"
logged=2
 
while logged != 0:
    while login != "Phil":    
            login = raw_input("Login : ")
    while password != "McChicken":
            password = raw_input("Password: ")
    logged = 1
 
    print "Welcome!"
    print "To leave type lock "
 
    while logged == 1:
        leave = raw_input (">> ")
        if leave == "lock":
            logged = 0
print "Goodbye!!"




 
Last edited by tab on Tue Aug 18, 2009 12:40 pm, edited 1 time in total.
<<

UNIX

User avatar

Hero Member
Hero Member

Posts: 1244

Joined: Mon Apr 28, 2008 9:20 am

Post Tue Aug 18, 2009 12:35 am

Re: Python Help

Hello tab and welcome to the forums.

When posting code snippets I would suggest to use the code tags [ code] insert code here [/ code] (without spaces) in order to keep the structure.

Are you sure that this is the whole program? Whats name and password in the first part? Just curious.


  Code:
login = "john" (0)
password = "tucker" (0)
logged=2
 
while logged != 0: (1)
    while login != "Phil":   (2)
            login = raw_input("Login : ") (2)
    while password != "McChicken": (3)
            password = raw_input("Password: ") (3)
    logged = 1
 
    print "Welcome!" (4)
    print "To leave type lock " (5)
 
    while logged == 1: (6)
        leave = raw_input (">> ") (7)
        if leave == "lock": (8)
            logged = 0
print "Goodbye!!"


(1): Do the following as long as logged is different from 0 (which is at this state always the case because logged was declared with 2).
(2): As long as the entered username is different from Phil, ask again for the correct username. As soon as Phil was entered, it goes to (3)
(3): As long as the entered password is different from McChicken, ask again for the correct password. As soon as McChicken was entered, logged is set to 1 and the lines (4) and (5) are printed.

I am not sure why login and password at the beginning (0) are declared with different strings than later used, but I assume it is because to demonstrate that if you declare a variable at a later point with something different, the latest declaration/ comparison is valid.

E.g.
  Code:
user = "tab"
password = "P()§"JND"

user = "tab2"
password = "ooooo"

print "user: " + user #displays user: tab2
print "pass: " + "password #displays pass: ooooo


(6): Do the following as long as logged is 1, which is at this point always the case, because it was before set to 1.
(7): Store all input done into leave (7). As soon as leave stores the string lock, logged is set to 0 which results in leaving the while loop (6) (because logged == 1 is not true anymore).

Hope you understand my explanations.
<<

tab

Newbie
Newbie

Posts: 2

Joined: Mon Aug 17, 2009 8:24 pm

Post Tue Aug 18, 2009 12:39 pm

Re: Python Help

awsec, sorry for not putting the code tags in I already fixed it. As for the program It's from the python wiki tutorial (http://en.wikibooks.org/wiki/Non-Progra ... ount_to_10). The whole program is stated under the "solution" part its the scond example of it.

But I did get what you mean though. But I can enter any username and password aside from what is written in the program and still manage to get in. That is the part that confuses me.


edit: Oh, I see where I did wrong its two different programs but I read it as one and yeah.. I'm sorry about this. Please disregard this topic and sorry for the newbie mistake  :(

tab-
Last edited by tab on Tue Aug 18, 2009 3:51 pm, edited 1 time in total.
<<

UNIX

User avatar

Hero Member
Hero Member

Posts: 1244

Joined: Mon Apr 28, 2008 9:20 am

Post Wed Aug 19, 2009 12:17 am

Re: Python Help

No need for apologizing. Glad you were able to proceed.

If you come across any other problems, feel free to ask.

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software