So I am doing some of the suggested readings here: http://tinyurl.com/5qv3yb and am trying to do an exercise about writing a password asking program. I was successful, but with one thing I can't figure out. When it asks for my password I have to put quotation marks " " around it or it says "name is X is not defined". How do I make it not need the quotations?
Note: I am using TextMate
Can you link your code? It shouldn't be a problem of the text editor, at least, hopefully not.
password = 'mozart' question = "What is your password? " print "Please enter your password to log in" x = input(question) while x != password: if x != password: print "Password Incorrect: 2 tries remaining" x = input(question) if x!= password: print "Password Incorrect: 1 try remaining" x = input(question) if x!= password: print "Password Incorrect. You have been denied access" quit() else: x == password print "You have succesfully logged in"
Try using codepad or something like that to post your code afterwards. It is easier to see and debug. Did you try raw_input instead of only input? raw_input returns a string.
I think you'll need a variable that iterates from three to zero as your unsuccessful tries happen, rather than nested ifs
pully6, yeah, that would be a lot better, but I wasn't sure how to do it, so I kind of just improvised something else that worked. BMP, thanks so much, that was what was wrong! I knew it was something simple. I'm still learning the basics here, haha.
No worries, I'm the same just learning basics. I'm working on a solution to your prob, starting with something like: for triesRemaining in range(0,3): raw_input(question) != password
Also, thanks for letting me know about codepad BMP. Good to know :).
Do you mind putting that code into context of the problem pully6? I don't understand where to plug it in. here is a link using codepad to what I have: http://codepad.org/SlDIF9Bm
I've re-written your problem, it works now. I surprised myself actually! Used an iterating variable triesRemaining to give the user 3 chances. http://codepad.org/Nxd7yYia
Other cleaner option is to set a boolean value as false and then, if the input matches the password, this boolean becomes True, exiting the loop. Something like while not isPass: #code if input == password: isPass = True. And, no problem mate, the beginning is harsh but it is worth it. And codepad is just to make easier to help you out / clearer to see the code.
ah boolean is a good idea. I found the learning curve for the first few lectures quite steep but now I'm up to lect 5 I'm making good progress. stick with it :)
Thanks a bunch for the help guys :). OpenStudy and the people on it make this process a lot easier.
raw_input() asks the user for some text and returns it as a string. That's what you want to use (almost always). input() asks the user for some text, then tries to interpret it as python code. That is dangerous. A malicious user can inject any arbitrary code into your program that way. Also, that's why it complained about not finding the name. If the user types a string with no quotes around it, they're really typing a variable name. If they happen to type the name of a variable you're using, then there won't be an error. If what they type isn't a variable name, then python doesn't know what they're trying to do, so it complains.
Join our real-time social learning platform and learn together with your friends!