How to repeat a code when its statement is false [Python] - 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')

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.

How do I correctly format putting an "if" in python

How would I put "if" or "else" in code like this?
user = input('Login: Username\n')
time.sleep(1)
password = input('Login: Password\n')
print('Welcome, %s.' % user)
I think this is what you want to do. But this is not a secure way of doing password requests.
user = input('Login: Username\n')
password = input('Login: Password\n')
users = {"You" : "1234", "Me" : "abcd"}
if user in users.keys():
if users[user] == password:
print("Welcome, {u}".format(u=user))
else:
print("Wrong password")
else:
print("This user does not exist")
Here is a simple version on how this should work but is not secure at all, but functions (almost) the same way as you requested (came up with this under 5 mins)
user = input('Login Username: ')
password = input('Login Password: ')
if user == ("Your username here"):
if password == ("Your password here"):
print("Welcome ", user)
Also you do not need "time.sleep()" as it only slows down your code

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")

python. how to add in .lower().

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

Categories

Resources