Jump to content

Python Help? Please.

Tohrchur

Hello! I'm trying to learn a bit of Python.  And I am doing "while" statements. and my task was to "Write a program to ask for passwords. If the user has entered

more than 3 passwords, print ‘security breach! Halt!’ and end the program.  If they get the password correct, print ‘Hello, [insert name here]! Welcome’.  and exit the program."
I attempted to do this the best I could, and it works sort of. There is probably a much better way to do this that I overlooked but this was the most logical to me at the time.
password = 'compsci'
a1=raw_input ("What is the password?")
a2=raw_input ("Incorrect, try again.")
a3=raw_input ("Incorrect, try again.")
while a1 or a2 or a3 != password:
    print ("Security breach! Halt!"*1)
while a1 or a2 or a3 == password:
    print ("Welcome, Cameron!")
 
then if you mess up the password 3 times the "Security Breach! Halt!" part goes into an infinite loop and I dont know how to get the statement to be said once, and also I dont know how to make this any simpler. If someone could help me that would be so great. Thanks!

 

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

im not an expert on python but:

 

if (a1 == password || a2 == password || a3 == password ) {    // || equals OR

print ( "Welcome and stuff" )

}

else {

print ("Halt!")

}

Personal Build Project "Rained-On"

helped building up the CPU Overclocking Database and GPU Overclocking Database, check them out ;)

#KilledMyWife #MakeBombs #LinusIsNotFunny || Please, dont use non-default grey font colors. Think about the night-theme users! ;)

Link to comment
Share on other sites

Link to post
Share on other sites

loop with a counter that counts to 3 and exits, I'm not sure if python can count though :P 

Link to comment
Share on other sites

Link to post
Share on other sites

I'll do it in C++ because I don't know Python well,

 

string pw="compsci";
string in;
cout << "What is the password?";
int count=0;
while(true){
      cin >> in;
      if(in.compare(pw)==0){
           cout << "welcome.\n"; 
           break;
      }
      else if(in.compare(pw) != 0 && count<3){
           cout << "Incorrect password.\n";
           count++;
      }
      else if(in.compare(pw) != 0 && count==3){
             cout << "Halt\n";
            while(true);
       }
}
 
when the proper condition is met for the infinite loop, just print the error statement once and then go into an infinite loop.
Link to comment
Share on other sites

Link to post
Share on other sites

You're over thinking this. I can't remember the proper inequality sign but I'll try.

 

while password =/= 'compsci':

 

then you make a for loop to run 3 times if the password is never 'compsci' print the halt string.

 

Hope that helps and makes sense.

i5 4670k | Sapphire 7950 | Kingston 120GB SSD | Seagate 1TB | G.Skill Ripjaw X Series 8GB

PB238Q | Steelseries Sensei | Ducky DK9087 | Qck Heavy

Build Log: http://linustechtips.com/main/topic/44902-from-imac-to-my-own-creation/

Link to comment
Share on other sites

Link to post
Share on other sites

You're over thinking this. I can't remember the proper inequality sign but I'll try.

 !=

Link to comment
Share on other sites

Link to post
Share on other sites

password = 'compsci'

counter = 1

While counter < 4:

    input = raw_input('Enter Password')

    if input == password:

        Print ('Welcome, Cameron!')

        //goto where ever

    else

        Print ('Incorrect, Try Again')

        counter += 1

Print ('Security Breach, Halt!')

 

try this, if it doesn't work or you need more help just PM me!

Link to comment
Share on other sites

Link to post
Share on other sites

aaaggghhhh! Please use the code tags for code so that it's much easier to read. It's the <> button just under the :) on the standard reply thing.

And I don't know python so I won't really be able to help you.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

OK so first I'll show you how I would do it. Please bear in mind that I am coding in Python 3.3, it may be some help if you could tell us what version of Python your using as "raw_input" was renamed to "input" as of 3.1. My code therefore may work with that simple change but I can't make any promises!

password="compsci"for counter in range (0,3):    attempt=input("What is the password?")    if attempt == password:        print("Welcome, Cameron!")        break    elif attempt != password:        print("Incorrect password!")if counter==2:    print("Security breach! Halt!")

The following paragraph is my explanation of some of the things I have done alternate to yours, if you understand all of the code you can just skip it.

 

I wrote it using a for loop as it makes more sense to do it this way as you have defined the number of attempts as "3", a for loop works in the structure of "for (put a string here) in range (the first number is the start of the range of the loop, the second is the end of the range)", in practice just look at the code above. You can use work around using the break function but you should always aim to use the minimal amount of code as possible in most cases. It is also good for future reference to use "elif" after you have done one statement of if (presuming you are interrogating the same string), if the if statement is true then the program can just skip all below it which helps if your writing long programs (again referring back to minimalism). The rest you should be able to understand, if not pm me or quote me in this thread.

 

OK now I'll do it using a single while loop.

password="compsci"c=0variable=Truewhile variable == True:    attempt=input("What is the password?")    if attempt == password:        print("Welcome, Cameron!")        variable=False    elif attempt != password:        print("Incorrect password!")        c=c+1        if c==3:            print("Security breach! Halt!")            variable=False

Now the program is pretty much the same infact you don't even need the 'variable=false', you can simply use 'break' again like in the previous piece of code. The while loop is alright in this case but personally I'd use the for loop as if in future you have a condition where it can just end the loop on its accord, you will have to sue break for the while loop where as a for loop can just finish at the end of the range. Also in this case you can utilize the for loops counting to tell you if the person has failed 3 attempts.

 

Sorry this is quite long but I thought I'd try and explain some things as well. If you ever need any more help, just pm me and I'll try and help (the only language I know is Python however so don't ask me about C++ otherwise I'll just shrug :) ).

| Cooler Master HAF 912 Plus | MSI P67A-G45 | i5 2500K @ 4.2GHz |  Coolermaster Hyper 212 Plus | EVGA GTX 670 FTW Edition 2GB | 8GB (2X4GB) Mushkin Blackline @ 1600MHz | 256GB OCZ Vertex 4 SSD | 1TB Western Digital Caviar Green | Corsair 600CX V2 | Windows 7 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

The code says it all

password = "thepassword"chances = 3while chances > 0:	#Gives the use three guesses	enteredPassword = input("Enter Password: ")	if enteredPassword == password:	#Check password is right		print("Welcome, John Smith")		break	#Break out of while loop	else:		chances -= 1	#Counts down when password is wrong		if chances == 0:	#Prints if 3 wrong passwords were entered			print("Security breach! Halt!")			print("Security breach! Halt!")			print("Security breach! Halt!")STOP = input("STOP!") #Only here to keep console open after the above finishes

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

You're over thinking this. I can't remember the proper inequality sign but I'll try.

 

while password =/= 'compsci':

 

then you make a for loop to run 3 times if the password is never 'compsci' print the halt string.

 

Hope that helps and makes sense.

I think you mean !=

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×