Lec 1-3.. Conditional Statements section of Python Programming http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements Bottom of the page under "If exercises", Question #1. I tried to do it without looking at the answer key. I'm brand new to thinking like a computer scientist so bear with me. I used the WHILE loop. And the result was the program saying "try again", "try again", "try again", "you've been denied", after inputting the wrong password. It did not ask me/the user for the password 3 different times because it's a WHILE loop instead of a FOR loop, right?
http://pastebin.com/YKYpQWg9 Here is the code. The program is up top along with getting the password correct, and getting the password incorrect. 2) Another issue that comes up is that after I input a password, the program will run and I will get a popup stating "Program is still running, kill it?". If I press OK, it will exit the Python IDE. Real annoying. If I press cancel, I get those errors. (shown in the pastebin).
It didn't ask you for the password three different times because your input() function is outside the loop. The input function is what prompts the user. You need to put it in the loop somewhere in order to ask the user more than once.
Thanks that worked for me. I learned something yet again. Here is the new program: http://pastebin.com/2dDXtu60 Why doe the answer key use a FOR loop? It says: For i in range (1,4): blah blah blah. And makes the loop go through the for loop and when i==3, it stops. It appears the end results are the same. My program with the WHILE loop seems to accomplish the same thing. Is there any benefit to using the FOR loop? Or different strokes for different people? Secondly, I noticed that if I remove exit() from the code, the Python IDE will not give me a popup stating that "the program is still running, kill it?" Does that mean that exit() is actually workng properly and that it is trying to close out of Python IDE??
I'm only a beginner myself. Exit will kill the program. a better way of exiting from a function is the 'return' statement. 'return' can also pass back the result of a function to which ever part of the program that called it
also using input is a bad idea. best to use raw_input() and then extract from the string what you need.
Well Python 3.2 doesn't have raw_input() if I'm not mistaken. I dont know enough to know the difference but I read that 3.2 made raw_input just input().
Join our real-time social learning platform and learn together with your friends!