-1

Just started python and racking my brains on this but can't seem to get it right.

print('Enter correct username and password combo to continue') count=0 password=Hytu76E username=bank_admin while password!='Hytu76E' and username!='bank_admin' and count<4: username=input('Enter username: ') and password=input('Enter password: ') if password=='Hytu76E' and username=='bank_admin': print('Access granted') else: print('Access denied. Try again.') count-=1 

syntax error, can't assign to operator on line 6 username=input.

3
  • format your code. you've while condition which is negating the if condition inside it. Commented Nov 9, 2017 at 12:53
  • 1
    change count-=1 to count+=1 and then remove the redundant username/password check in the while loop Commented Nov 9, 2017 at 12:54
  • 1
    Shouldn't the values password and username be inside quotes, in the beginning? Commented Nov 9, 2017 at 12:57

9 Answers 9

5

Fixed the code to achieve what you are trying to do:

print('Enter correct username and password combo to continue') count=0 while count < 3: username = input('Enter username: ') password = input('Enter password: ') if password=='Hytu76E' and username=='bank_admin': print('Access granted') break else: print('Access denied. Try again.') count += 1 

Changes that have been made:

  • Removed the definition of username and password since it is redundant and can be omitted
  • Changed the while statement to count 3 iterations of count
  • Validation of the credentials only in the if statement and not in the while
  • Changed the decreasing of count to increasing (from count -= to count +=)
  • break the loop when the right credentials are entered
Sign up to request clarification or add additional context in comments.

1 Comment

That's better, but you still have those redundant assignments. They don't do anything useful, they just waste space & time.
2

here try this (I try to change your code as less as possible so that you can identify the same logic yourself)

print('Enter correct username and password combo to continue') count = 0 # "" or '' because you are assigning a value string into it password = "" username = "" # looping will continue when wrong input for three times and ask again... while password!='Hytu76E' and username!='bank_admin' and count < 3: # you are collecting user input from CLI separately (you can not assign and operator to such operation as per your code ;) username = input("Enter username: ") password = input("Enter password: ") if password=='Hytu76E' and username=='bank_admin': # if match, grand and break print('Access granted') break else: print('Access denied. Try again.') count+=1 # as per gbse, in the comments, you will need the + to count up 

issues in your code:

# you are assigning string value, what for? this would make the loop hit positive the first time password=Hytu76E # string assignment error in syntax, anyway username=bank_admin # string assignment error in syntax, anyway # you can not assigning and operator in the input because of no if condition in this line, also you should compare the values of the input username=input('Enter username: ') and password=input('Enter password: ') # if code is ok, then move outside the loop in the case when the user enters the first time good answers if password=='Hytu76E' and username=='bank_admin': print('Access granted') else: print('Access denied. Try again.') # you are decremented the counter which would never leave teh loop at 4, you should add one on each iteration so count+=1 (count = count + 1) count-=1 

Comments

1

You can use for loop:

#!/usr/bin/python3 for _ in range(3): usr = input("Enter username: ") psw = input("Enter password: ") if usr == "bank_admin" and psw == "Hytu76E": print("Access Granted!") break else: print("Access Denied!") print("Try Again!") else: print("No more attemps!") 

Comments

0

I think this is what you're looking for: Accept username and password and verify it against a particular one mentioned in the code, with a max try limit of 3

print('Enter correct username and password combo to continue') count=1 while count<4: username=input('Enter username: ') password=input('Enter password: ') if password=='Hytu76E' and username=='bank_admin': print('Access granted') count=5 else: print('Access denied. Try again.') count+=1 

1 Comment

This does not properly re-evaluate the inputs repeatedly.
0

Firstly you can remove the initial definition you gave to password and username at the start as well as changing the while loop to become while count<4

So it would look like:

print('enter the correct username and password combo to continue') count = 0 while count<4: 

If we had kept it how it was previously it would be unnecessary and clutter your program more.

To fix your syntax error you need to remove the and placed inbetween username and password, so the middle will look more like this:

username = input('Enter username: ') password = input('Enter password: ') 

Then at the end you want to change count-=1 to count+=1, because if it takes one away every time it will never hit 4 and your loop will be infinite, which is not what you are trying to achieve.

Here is the entire fix:

print('Enter correct username and password combo to continue') count=0 while count<4: username=input('Enter username: ') password=input('Enter password: ') if password=='Hytu76E' and username=='bank_admin': print('Access granted') count=5 else: print('Access denied. Try again.') count+=1 

Here is a list of changes I have made:

  • Removed the password and username definition in lines 3 and 4

  • Changed your while loop to become while<4

  • Removed the and inbetween username=input and password=input

  • Added count=5 after if statement so that the loop ends

Comments

0

I mean the idea behind this:

password=Hytu76E username=bank_admin while password!='Hytu76E' and username!='bank_admin' and count<4: 

seems to be that you get into the loop. But why so complicated? You could also just start a loop that runs 3 times:

for i in range(3): [do something] 

And in terms of what the [do something] could be. Well first of all you need to check the user input:

username=input('Enter username: ') and password=input('Enter password: ') 

So the idea is good but what you do is you request 2 inputs in the same statement and then COMPARE them with an AND statement. So no wonder the interpreter gets confused here. What you probably wanted to do instead is just write them on two separate lines:

username=input('Enter username: ') password=input('Enter password: ') 

if you really want to/need to do it on one line you could use:

username, password = input(), input() 

Then you'd need to insert "[Your Name][ENTER]" "[Your Password][ENTER]", but although it would work I'd not recommend it as it's probably nothing but confusing to both you and the potential user.

Next up you'd need your condition as it's no longer part of the loop:

if username == [username] and password == [password]: print('Access granted') break else: print('Access denied. Try again.') 

Here break skips the rest of the loop once the condition is met. If you want to be fancy you can also add a condition to check whether it's the last try:

else: if i < 2: print('Access denied. Try again.') else: print('Access denied. IP was added to the log') 

Comments

0

Not sure you want this, but you can split the inputs of Username and Password:

count = 1 while count < 4: username = input('Enter username: ') if username == 'Sdfh123': print('OK! Enter your Password:') count = 5 else: print('Access DENIED. Try Again:') count+=1 count = 1 while count < 4: password = input('Enter Password: ') if password == 'swordfish': print('ACCESS GRANTED!') count = 5 else: print('Access DENIED. Try Again:') count+=1 

Comments

-1
user = 'Mike' pw = '1234' print('Please enter your username and password: ') username = "" password = "" count = 0 while username != user and password != pw and count < 3: username = input('Username: ') password = input('password: ') if username == user and password == pw: print('Welcome',username) else: print('Denied, Please try again') count = count + 1 

2 Comments

Please include an explanation of how this answer provides additional useful information that is not covered in multiple other answers.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

what is wrong with my code? every time i run it and input the set username it just makes me input my username again.

import time username = "229796" password = "rwqfsfascx" yes = "update my system" no = "continue to browser" count = 0 while count < 3: user_choice = input('Enter username.') if user_choice == username: print('Remember this code: 17283645.') count = 4 else: print('Login unsuccessful, please try again.') count += 1 time.sleep(2) # Pause for 2 seconds while count < 3: user_choice = input('Enter password.') if user_choice == password: print('Access granted.') count = 4 else: print('Login unsuccessful, please try again.') count += 1 print('Initiating system start. Please wait...') time.sleep(3) # Pause for 3 seconds print('System activated.') time.sleep(1) # Pause for 1 second user_choice = input('Would you like to update your system or continue directly to the browser?') if user_choice == yes: print('Beginning system update. Please wait.') time.sleep(7) # Pause for 7 seconds print('System update complete. Opening browser.') elif user_choice == no: print('Opening browser.') else: print('Error 136. Please reload the system.') time.sleep(2) print('Browser activated. Opening .pjycs37 coding platform.') time.sleep(2) user_choice = input('Choose a programming language.') print('Activating', user_choice, 'code platform.') time.sleep(3) print(user_choice, 'code platform activated.') 

2 Comments

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
Please ask the question in different thread (Ask Question).