python. how to add in .lower(). - python

user1 = "aqa code"
pass1 = "uzair123"
username = input("Enter username: ")
password = input("Enter password: ")
while username != user1:
print("Access denied: username is incorrect try again ")
username = input("Enter username again: ")
if username == user1:
print("access granted")
while password != pass1:
print("Password is still incorrect")
password=input("Enter password again: ")
if password == pass1:
print("Access granted")
while password != pass1:
print("Access denied,Password is incorrect")
password=input("Enter password again: ")
if password == pass1:
print("Access granted")
How do I add a .lower() to the username/user1 so it becomes case insensitive when you type in the answer? Please help.

You can call .lower on the return of the input function.
username = input("Enter username: ").lower()
password = input("Enter password: ").lower()

Adding .lower() is indeed the solution you're looking for, but you may want to try to simplify your flow a bit, so you have fewer places to edit whenever you want to modify your input statement. To do this I would suggest creating a variable for each loop that is initially false and only get user input once inside each loop:
loop_condition = False #initialize loop condition
while not loop_condition: #loop until we say so
value = input("please enter a value: ") #human input
value = value.lower() #convert to lower case for comparison
if value == "password": #test
print("success")
loop_condition = True #loop will terminate when we hit our `while` again
else:
print("fail")
print("rest of program goes here")
Here you can see we only call input() once, and only have to convert it to lowercase in one place as well. Simplicity is always your friend.
note: use raw_input() if you're using python 2.7

Related

indented block after 'while' statement, dont know what to change to make it worth

while True:
print("please enter username")
input()
username = input()
if username == "Elias":
break
print("username correct")
break;
while True:
print("please enter password")
input()
password = input()
if password == "1234567890":
break
print("access granted");
print("rechner:")
variable_a = input("alle noten mit + zeichen eintippen")
print("Dein Durchschnitt ist", input()/2);
File "C:\Users\Odyesp\PycharmProjects\pythonlearning.py\main.py", line 3
print("please enter username")
^
IndentationError: expected an indented block after 'while' statement on line 1
I feel that you may be very new to Python, and that's perfectly ok! We were all really new at some point.
For your use case, I think it's important to step back and analyze what you're wanting to do.
Capture a username
Continue trying to capture a username until it is "Elias"
Capture a password
Continue trying to capture a password until it is "1234567890"
At the end, print "access granted" and "rechner:"
Here's some code:
# Capture username, and only proceed if "Elias"
username = ""
while username != "Elias":
username = input("Please enter your username: ")
print("username correct")
# Capture password, and only proceed if "1234567890"
password = ""
while password != "1234567890":
password = input("Please enter your password: ")
print("access granted")
print("rechner:")
Here's an example of the output from the terminal, along with my inputs

Trouble looping back to certain point and quitting program on 3 wrong guesses

while True:
print('enter username: ')
username = input()
if username.lower() != 'joe':
print("imposter!")
continue
print(f'Hello {username.capitalize()}')
print('enter password: ')
password = input()
tries = 0
if password != 'Water':
tries += 1
continue
if tries == 3:
print("3 strikes, you're out")
quit()
else:
break
print("access granted")
Trying to make a username and password prompt. I am trying to give infinite tries to username entries, and only 3 chances for a correct password. When you type in the correct user name and password everything works fine. BUT when typing in the incorrect password, it loops back up to enter username, and the 'tries' counter does not work. python noob trying to learn using Automate The Boring Things in Python
You could try restructuring your code like the below:
import sys
access = False
while not access:
username = input('Enter username: ')
if username.lower() != 'joe':
print("imposter!")
continue
else:
print(f'Hello {username.capitalize()}')
for i in range(3):
password = input('Enter password: ')
if password == 'Water':
access = True
break
else:
print("3 strikes, you're out")
sys.exit()
print("Access granted")
You reset tries = 0 within the loop.

If statement in Python always return true

I am trying to write a program which will ask my name and password before giving me access. Somehow I am not writing it right
print("Enter your name:")
myName = input ()
if myName == "Akib":
print("Enter your password")
password = input()
password = "toma"
if password == "toma":
print("Access granted")
else:
print("Not granted")
The issue is you set password to the input and then rest password by setting it to toma so you need to remove password = "toma".
print("Enter your name:")
myName = input ()
if myName == "Akib":
print("Enter your password")
password = input()
if password == "toma":
print("Access granted")
else:
print("Not granted")
password = "password" # store your password here
name = "name" # store your name here
input_name = str(input("> enter your name: "))
input_password = str(input("> enter your password: "))
if input_name == name and input_password == password:
print("access granted !")
else:
print("declined !")
It can be done like-so. Hope this helps :)
You could achieve your desired function like so!
In this case, if the name entered is not 'Akib', it would not even prompt the user to enter the password.
# prompt for name
myName = input("Enter your name: ")
# check if name is correct
if myName == "Akib":
# prompt for password
password = input("Enter your password: ")
# check if password is correct
if password == "toma":
print("Access granted")
else:
print("Not granted")
else:
print("Not granted")

Confirm user and password from a list of class objects

I need to be able to validate the user and the password inputted, but when I run the code below, I'm able to verify only the first element of the list and the second element and so on aren't being verified.
Note: The user and password are stored in the list as class objects [like this:
admin(user, password)]...
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
for obj in self.admins:
while obj.admin_name != user_name and obj.admin_password != password:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
else:
print("Greetings,", user_name, "You are Now Logged in the System")
break
When you run break in your else branch you are actually calling it on the for loop. Remove the break and it should be working as you expect it to
Your while loop only checks for the first user name. You should switch the order of your loops:
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
while True:
for obj in self.admins:
if obj.admin_name == user_name and obj.admin_password == password:
break
else:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
continue
break
print("Greetings,", user_name, "You are Now Logged in the System")
This is also a very bad way to check passwords.
Simply remove break from your else statement.

How to repeat a code when its statement is false [Python]

I really want to learn how do I repeat my code when its statement is false in Python. I just wanna know, how do I make Python ask for username and password again if its wrong? Here is my code:
print("Enter username")
username = input()
print("Enter password")
pword = input()
if username=="Shadow" and pword=="ItzShadow":
print("Access granted to Shadow!")
else:
print("Wrong username and / or password")
I meant, how can I make Python ask for Username and Password again if one of it is false?
You need to enclose your code in a loop. Something like this:
def get_user_pass():
print("Enter username")
username = input()
print("Enter password")
pword = input()
return username, pword
username, pword = get_user_pass()
while not(username=="Shadow" and pword=="ItzShadow"):
print("Wrong username and / or password")
username, pword = get_user_pass()
print("Access granted to Shadow!")
Use a while loop:
while True:
print("Enter username")
username = input()
print("Enter password")
pword = input()
if username=="Shadow" and pword=="ItzShadow":
print("Access granted to Shadow!")
break # Exit the loop
else:
print("Wrong username and / or password") # Will repeat
stuff() # Only executed when authenticated
We are using the data entering function get_input in the loop each time the user and password aren't same.
def get_input():
# enter your declarations here
return [user_name,password]
user,pwd=get_input()
while (user!='your desired user' and pwd!='the password'):
print('Not authorized')
user,pwd=get_input()
print('Your success message')

Categories

Resources