Python Newbie

B

Brian

Hi I am new to this newsgroup and new to programming. I have had this
interest in learning to program because.......well just because. It's
just a curiosity that I have. The reason I am starting with Python,
well some of the websites I went to recommended Python, so here I am.
Ok on to the question.

I am currently using the book "Python programming for absolute
beginners" by Michael Dawson. The program I am working on is a
password program....dont laugh I know it's really simple to some, but
I am kinda stuck. The book shows how to code a password program like
this:

password = raw_input("Enter your password:")

if password == "secret":
print "Access Granted"
else:
print "Access Denied"

raw_input("Press the enter key to exit")

Ok that is easy enough I understand what is going on here. As the
book goes on it also talks about elif, and while loops. At the end of
the chapter it gives various challenges that the reader is asked to
complete. One of the challenges is to take the password program from
above and change it so that the user gets only three attempts to enter
the correct password. Well I ain't got it to work yet, I thought I
came close, but no cigar. This is what I have so far:
#Limiting password atempts to three

password = raw_input("Enter your Password: ")
count = 0


while password != "secret":
print password = raw-input("Enter your Password: ")
count += 1

if password == "secret":
print "Welcome in."


elif count > 3:
print "Please come back when you remember your Password."

else:
raw_input("Press enter to exit.")

Ok, I know I may be way off course here but any help would be greatly
appreciated. I dont want to get frustrated and say the heck with it,
especially if it is something minor. Thanks in advance.
 
J

Jorge Godoy

#Limiting password atempts to three

password = raw_input("Enter your Password: ")
count = 0


while password != "secret":
print password = raw-input("Enter your Password: ")
count += 1

if password == "secret":
print "Welcome in."


elif count > 3:
print "Please come back when you remember your Password."

else:
raw_input("Press enter to exit.")

Ok, I know I may be way off course here but any help would be greatly
appreciated. I dont want to get frustrated and say the heck with it,
especially if it is something minor. Thanks in advance.

I won't end your fun giving you the answer, but remember that in Python
indentation matters, i.e., your lines alignment matters.

Read the program above and remember that when you go further on indentation,
you just come back when the block is ended.


Got it?
 
E

EAS

Hey, I'm using the same book! : )

I'm a newbie too, but I think I can still help you.
Let's take a look at your code:

#Limiting password atempts to three

password = raw_input("Enter your Password: ")
count = 0


while password != "secret":
print password = raw-input("Enter your Password: ")
count += 1

if password == "secret":
print "Welcome in."


elif count > 3:
print "Please come back when you remember your Password."

else:
raw_input("Press enter to exit.")

The first error I spot is in the loop - "raw-input" should be "raw_input".
"elif count"
should be == 3, not more than 3. Also, you do not need "print" before
"password".
If you want to view one of the messages before it closes out, you must take
the last
statement out of "else" and make it its own. Otherwise, it won't run that
statement.

The rest is just the structure of the program... according to the loop, the
user
must enter passwords until he/she types in the word "password", where the
statement
password != "secret" becomes false. Only then will the program move onto the
"if"
statement and tell the user whether they are granted access or not. To end
the loop
when the user guesses 3 times, you need to add another "if" statement inside
the loop
that breaks it if the count is equal to 3. Here is the new program:

#Limiting password atempts to three

password = raw_input("Enter your Password: ")
count = 0


while password != "secret":
if count == 3:
break
password = raw_input("Enter your Password: ")
count += 1

if password == "secret":
print "Welcome in."

elif count == 3:
print "Please come back when you remember your Password."

else:
raw_input("Press enter to exit.")

Remember to go step by step through the program when you have errors
and see exactly what it does.
 
L

Larry Bates

As Jorge points out, indentation matters. Try
something like:

import sys
count=0
while 1:
password = raw_input("Enter your Password (blank to exit): ")
count += 1

if password == "secret":
print "Welcome in."
break

elif not password: sys.exit()

else:
if count < 3:
print "Wrong password"
continue
else:
print "Please come back when you remember your Password."
sys.exit()
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,115
Latest member
JoshuaMoul
Top