Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I tried to create a username and password program for my family, but when I open it it says:
Welcome.
Denied!!!
Press enter to quit.
Can anyone solve my problem? The code is below:
print ("Welcome.")
username = ("Please enter your username.")
if username == ("mom, savanna, joseph"):
print ("Granted.")
password = input ("Please enter your password.")
if password == ("1975, 2000, jesus"):
print ("Granted.")
question = ("Do you like it?")
if question == ("yes"):
print ("I thought you would.")
if question == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
else:
print ("Denied!!!")
input ("press enter to quit")
Your primary issue is on this line:
username = ("Please enter your username.")
I presume you mean to have an input function call there.
username = input("Please enter your username.")
That will solve your immediate rejection issue.
You also have the same issue on this line:
question = ("Do you like it?")
Which should also be:
question = input("Do you like it?")
Here's the thing:
You are new to code. We can see that here. However, there is always room for improvement and you can learn from mistakes. Some people make odd code sometimes ;)
I have created a working model for you to see where you went wrong.
print ("Welcome.")
username = input("Please enter your username.")
if username in ("mom", "savanna", "joseph"):
password = input("Please enter your password.")
if password in ("1975","2000","jesus"):
print ("Granted.")
question = input("Do you like it?")
if question == ("yes"):
print ("I thought you would.")
elif question == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
break
You typed question = ("Do you like it?") without the input. This is important since it actually lets the user type something.
You wrote if username == ("mom,savanna,joseph")
this means that if the username is mom,savanna,joseph then you get in. obviously you wanted only one of these to get in. the Boolean operator or can help a lot in this situation.
Notice how I used elif instead of if. Python can only have one if and one else. but as many elifs as you want. It means else-if.
EDIT:
After reading Joel's comment, I noticed that you probably want Mom's password to be 1975, Savannah's to be 2000, and Joseph's to be jesus. How do you think you can do that by using my code given here?
I'm assuming you want to have 3 different usernames, and mom's password to be 1975 etc.
password = {"mom":"1975", "savanna":"2000", "joseph":"jesus"}
print ("Welcome.")
username = input("Please enter your username.")
if username in password: #if x in dict returns True if dict has the key x
print ("Granted.")
mypassword = input ("Please enter your password.")
if mypassword == password[username]:
print ("Granted.")
answer = input("Do you like it?")
if answer == ("yes"):
print ("I thought you would.")
elif answer == ("no"):
print ("I guess I could do better")
else:
print ("error")
else:
print ("Denied!!!")
else:
print ("Denied!!!")
input("press enter to quit")
You may want to put a loop around it so that it doesn't have to get restarted if there's a bad entry.
you are asking if username == ("mom, savanna, joseph") instead of if username in ("mom, savanna, joseph")
if you entered in "mom" for username when it asked you, it would be like comparing this
if "mom" == ("mom, savanna, joseph"):
("mom, savanna, joseph") and "mom" are not equal
to check if an item is in a list use the in keyword
>>> x = [0, 1, 2, 3, 4]
>>> 3 in x
True
you also did the same thing when checking if password ==
also #Alexander O'Mara is correct, you never did the input() for username
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to make a password system is python but when I tried to compere the user password to the right password I get an exception
'int' object is not subscriptable
import time
user_pass = input("Enter password here: ")
if len(user_pass) == 4:
print("u entered the correct amount of numbers!"), time.sleep(0.5)
else:
print("wrong password!")
exit()
for i in user_pass:
if not i.isdigit():
print("u must enter numbers only! \"{}\" is not a number!!!!!!!".format(i))
exit()
password = 1234
if (user_pass[0]) == str(password[0]):
print("the first number is correct")
else:
print("the first number is wrong!")
exit()
A couple of things here;
Firstly you need to indent your if not i.isdigit() block:
if not i.isdigit():
print("u must enter numbers only! \"{}\" is not a number!!!!!!!".format(i))
exit()
You will probably get an exception otherwise.
For your issue; it looks like you are setting the value of password = 1234 which is a integer. You can set this to password="1234" or str(password)[0].
the error occurs because in the line (user_pass[0]) == str(password[0]) you are trying to take the first character in an integer value, you can do this only with strings, so to fix your problem you should modify this (user_pass[0]) == str(password[0]) to this: str(user_pass)[0] == str(password)[0]
so the script should look like this:
import time
user_pass = input("Enter password here: ")
if len(user_pass) == 4:
print("u entered the correct amount of numbers!")
time.sleep(0.5)
else:
print("wrong password!")
exit()
for i in user_pass:
if not i.isdigit():
print("u must enter numbers only! \"{}\" is not a number!!!!!!!".format(i))
exit()
password = 1234
if str(user_pass)[0] == str(password)[0]:
print("the first number is correct")
else:
print("the first number is wrong!")
exit()
I've tested your code and formatted it again. there was some minor issue regarding indent and string casting, check this out
import time
user_pass = input("Enter password here: ")
if len(user_pass) == 4:
print("u entered the correct amount of numbers!"), time.sleep(0.5)
else:
print("wrong password!")
exit()
for i in user_pass:
if not i.isdigit():
print("u must enter numbers only! \"{}\" is not a number!!!!!!!".format(i))
exit()
password = "1234"
if (user_pass[0]) == password):
print("the first number is correct")
else:
print("the first number is wrong!")
exit()
I have an if (else) statement in my code and but when I attempt to execute the code it does not run, but I do not get a notification that there is a syntax error etc. I think I may be missing something but I am not sure what...
This is my current code:
NewUserOrOldUser= input ("Have you played this game before?")
if NewUserOrOldUser is "yes":
VerifyUn= input("Please enter the username you chose")
VerifyPw= input("Please enter the password you chose")
VerfifyUnPw= VerfiyUn + VerifyPw
if VerifyUnPw in open("Un & Pw").read():
print ("Proceed")
else:
print ("The data you inputted was incorrect, please try again")`
if NewUserOrOldUser is "no":
This is what happens when I run the code:
image of my what happens when my code runs, the first question is asked but nothing happens after that
You use if NewUserOrOldUser is "yes":
To make your code run use if NewUserOrOldUser == "yes":
Also remove the last else block or put code there.
NewUserOrOldUser= input ("Have you played this game before?")
if NewUserOrOldUser == "yes":
VerifyUn= input("Please enter the username you chose")
VerifyPw= input("Please enter the password you chose")
VerfifyUnPw= VerfiyUn + VerifyPw
if VerifyUnPw in open("Un & Pw").read():
print ("Proceed")
else:
print ("The data you inputted was incorrect, please try again")
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I'm very new at Python, and I'm trying to make a simple program that asks the user for a password, and if the user didn't guess the password they were asked, ask again until they guess it. How do I do that?
Password = input("guess the password: ")
while (password) != "12345":
Print(input("try again : "))
Welcome to Programming and StackOverflow. Have a look a this Example,
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('Loop ended.')
The break statement ends the while loop.
#g23's Answer is more apt in the context of the question
Make sure your capitalization is right, generally variables are lowercase, but you need to be consistent.
Also when you ask for the password again, you need to store what the user gives you so it can be checked in the loop condition (the while password != "12345": part)
Something like
password = input("Enter the password: ")
while password != "12345":
password = input("try again: ")
This code does what you want. It has a while loop to check if the password was guessed until the right password is entered. Then it has an if statement to write a message: if the right password is entered, it writes it.
password = input("Enter the password: ")
while password != "12345":
password = input("try again: ")
if password == "12345":
print("Correct password!")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to create a simple database, which will store and return data.
The problem is that continue doesn't seem to work.
Once the else statement in function checker is executed it keeps printing "try again" and seems to be deadlocked.
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
#If password is not strong enough try again
else:
print('Try Again')
continue
#Starts main program
while True:
database = {'dom':'yeet'}
welcome = input('Welcome! To login press L, if you are new press R: ')
#Login will log basically check if your info is in dictionary
if welcome == 'L':
for user in database.keys():
u = input('Username: ')
if u in user:
for passw in database.values():
p = input('Password: ')
if p in passw:
print('Welcome ' + str(u))
break
#Will create new user in dictionary
elif welcome == 'R':
u = input('Choose your username: ')
p = input('Choose your password: ')
#Runs checker function
checker()
database[u] = p
print('Welcome ' + str(u))
break
else:
continue
rewrite checker() like this:
You used incorrect indentation for the else block, and it was out of the loop.
def checker():
#Checks if the password is strong enough
while True:
if (p[0].isupper() and not p.isalpha() and len(p) > 7):
print('Password Created!')
break
else:
print('Try Again')
continue
The beauty of Python coding is you must indent code properly, keeping that in mind , automatically everybody writes beautiful code, and you'll never see a Python code with bad formating.
Indent your else part and you are good to go.
I'm fairly new to Python and am currently just learning by making some scripts to use at work. Real simple, just takes user input and stores it in a string to be called later on. The questions are yes/no answers but I wish for the user to have the option to skip and for the question to be asked again at the end, how would I do this?
Currently this is what I've got:
import sys
yes = ('yes', 'y')
no = ('no', 'n')
skip = ('skip', 's')
power = str(raw_input("Does the site have power? (Yes/No): "))
if power.lower() in yes:
pass
elif power.lower() in no:
pass
elif power.lower() in skip:
pass
else:
print ''
print '%s is an invlaid input! Please answer with Yes or No' % power
print ''
exit()
then at the end of the script after all the questions have been asked I have this:
if power.lower() in skip:
power = str(raw_input("Does the site have power? (Yes/No): "))
if power.lower() in yes:
pass
elif power.lower() in no:
pass
else:
print ''
print '%s is an invlaid input! Please answer with Yes or No' % power
print ''
exit()
else:
pass
if power.lower == 'yes':
print 'Site has power'
else:
print 'Site doesnt have power, NFF.'
I understand this is very messy and I'm just looking for guidance/help.
Regards,
Trap.
Since you're rather new to Python I'll give you some tips:
Store all the questions which receive "skip" as a response into a list.
At the end of all your questions, iterate through (hint: "for" loop) all the questions which the user skipped and asked them again.
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
message=("is this correct")
Print=(message)
#store input
answer = input("yes/no:")
if answer == "yes": print("thank you")
if answer == "no": print("hmm, are you sure?")
#store input
answer = input("yes/no:")
if answer == "yes" : print("please call my suppot hotline: +47 476 58 266")
if answer == "no" : print("ok goodbye:)")
if someone had a solution for line 7 when answerd yes skiped to the end desplaing ok `goodbye:) thank you`