I am trying to make a simple password checker in Python. It should accept a password if it has more than 1 upper and lower case letter and if it is more than 8 characters long. But when I run this I get the "do not continue" option from the if statement. Is there something wrong with the if statement?
passwd1 = input("enter your password>>>")
passwd2 = input("re-enter your password>>>")
lower=0
upper=0
for i in passwd1:
if(i.islower()):
lower=lower+1
elif(i.isupper()):
upper=upper+1
if lower + upper >= 8 and lower<0 and upper<0 and passwd1 == passwd2:
print("continue")
else:
print("do not continue")
Thanks
you will always go to the else block because this condition and lower<0 and upper<0 will never be met ,since you initiate lower,upper to 0 and only increase them in you if : lower=lower+1 , upper=upper+1 ,
and why do you need this condition anyway??? i don't see the purpose of this condition , remove this condition and lower<0 and upper<0 and it will work!
Related
So I was trying to use a procedure(with a parameter)...I asked the user for inputs and created a validate function to check the inputs and see if they are strings...I checked it but the outputs are taking too long to output. How do I fix this?
I tried:
# Create Validate function
def validate_input(LETTER):
while True:
try:
if len(LETTER) == 0:
pass
except:
if len(LETTER) >= 2:
print('Sorry, please enter a single letter')
if LETTER.strip().isdigit():
print('Sorry, please enter a letter')
break
#Ask for inputs
# Create function to validate input that returns true or false. If false then ask for input again.
first_char = input('Enter first character(lower cases) or press Enter: ')
validate_input(first_char)
second_char = input('Enter second character(lower cases) or press Enter: ')
validate_input(second_char)
third_char = input('Enter third character(lower cases) or press Enter: ')
validate_input(third_char)
fourth_char = input('Enter fourth character(lower cases) or press Enter: ')
validate_input(fourth_char)
fifth_char = input('Enter fifth character(lower cases) or press Enter: ')
validate_input(fifth_char)
But it came out to be:
Enter first character(lower cases) or press Enter: 2
And from there it takes too much time to say it it must be a string...
Thank you in advance!
the input() function in python always take the inputs as string, if you want to get integer as input then the following functions would be used => int(input())
it is not taking too long to execute it just a logical error in your code ;)
lets say the the input is "hello" so the len("hello") is 5
now , the input goes to the validation function(your function) first it starts with infinite while loop with no terminal condition and starts to checks the condition len(LETTER) == 0 whether it is true or false but it won't raise any exception so it won't go to the except block where the actual terminal condition is located(break) so its keep running forever.
hope it'll be helpful for you, thank you.
I'm a little new to Python so I apologise in advance if this is a really simple question but I have been trying to wrap my mind around this problem for a while now.
Simply put, my code is to prompt the user if they have written down a couple of values correctly, If they have I want the loop to continue so the values can be saved to a database. If they have not I would like the code to loop back to the top of the while loop so they can re-enter the values.
For some reason when I enter 'no' It goes through the loop regardless, how may I fix this?
Here's the code below:
while True:
clear()
print("\nPlease input the required data.\n")
in_name = input('Name: ')
in_email = input('Email: ')
in_address = input('Address: ')
clear()
print("\nDoes this look correct?\n")
print("#--START--#\n")
print("Name: " + in_name)
print("Email: " + in_email)
print("Address " + in_address)
print("\n#---END---#\n")
validate == input(">>> ")
if validate == "no":
continue
elif validate == "yes":
print("/nAttempting to copy to the database...")
cursor.execute("""
INSERT INTO contacts(name, email, address)
VALUES (?,?,?)
""", (in_name, in_email, in_address))
conn.commit ()
print ( 'Data entered successfully.\n' )
break
(I should note that this write program is part of a larger program, the loop is nested within another loop that acts as the main menu, perhaps that may help.)
the keyword continue will take you back to the next iteration without finishing the current one, example.
for i in range(5):
if i == 2:
continue
print(i * 5)
This means that when i is 2 it wont print(i=2 * 5), instead it will go up to start the next loop where i=3. The output will be
0
5
15
20
If you use break, it will just completely stop and exit out of the iteration once it reaches this keyword.
I think you're looking for the keyword pass.
'while True:' will always be True, meaning the loop will go on forever. I believe that you'll need to break out of the loop for it to stop. Based on Jorge Alvarez's answer, even changing 'continue' to 'pass' will allow the loop to go forever.
If I understand correctly, you'll need to change the initial 'while True:' statement to test whether validate equals 'no' or 'yes'.
I have a little piece of code in Python where I'm trying to compare a user input to a specific element in an array. Here is the code:
movies = ["movie 1", "movie2", "movie3"];
answer = raw_input("What is your guess: ")
if answer == movies[1]
then print ("yes that is correct")
else:
print ("no that is incorrect")
I know the indentation above looks wrong becasue I typed it out in the text box and I'm new to this site as well as python.
I also know that I probably need to use some sort of conditional loop, maybe a while loop, but I'm having trouble finding where I can compare user input string value to a string value in my array. Any ideas how I might accomplish this?
Have fun with Python! I guess you are trying to make a loop which keeps receiving inputs from user to compare with the desired input until user types the correct input. If so, one way, it can be implemented as following (but think of adding a break condition, like input == "Bored" , to avoid infinite loop and hard stopping your code):
movies = ["movie 1", "movie2", "movie3"]
correctAnswer = movies[1]
is_notCorrect = True
while(is_notCorrect):
answer = raw_input("What is your guess: ")
if answer == correctAnswer:
print("Yes, that is correct")
is_notCorrect = False
else:
print("No, that is incorrect")
In the code above, when is_notCorrect turns into False. At next condition checking, it will break condition, and done with the loop.
Your code has some issues
movies = ["movie 1", "movie2", "movie3"]; # No need the semi-colon in Python
answer = raw_input("What is your guess: ")
# Need a colon here after if condition, new line, and indent.
#If you don't like the colon, you need to write a different way with one line of code Eg: <Do A> if <Condition happens> else <Do B>
if answer == movies[1]
then print ("yes that is correct") # No then in if-else statement in Python
else:
print ("no that is incorrect")
I'm taking an intro Python course and a little stuck on an assignment. Any advice or resources would be greatly appreciated!
Here's the problem:
Write a program in Python that will prompt the user to enter an account number consists of 7 digits.
After getting that account number from user, verify if the account is valid or not. You should have a list called current_accts that hold all valid accounts.
Current valid accounts are shown below and you must use them in your program.
5679034 8232322 2134988 6541234 3984591 1298345 7849123 8723217
Verifying the account number entered should be done in a function called check_account() that will accept the account entered by the user and also the list current_accts. This function should return a 1 if account is valid otherwise return 0 if account is not valid.
Here's what I've written so far, but I'm stuck and also receiving syntax errors for indentation in lines 6-15. I'm also receiving error messages saying that variable 'current_accts' is not defined.
prompt = "Please, enter the 8 digit account number: "
current_accts = current_accts[1:]
current_accts [-1] = "valid"
while True:
try:
userinput = current_accts(prompt)
if len(userinput ) > 8:
raise ValueError()
userinput = int(userinput)
except ValueError:
print('The value must be an 8 digit integer. Try again')
else:
break
userinput = str(userinput)
a =int(userinput[7])+int(userinput[5])+int(userinput[3])+int(userinput[1])
b1 = str(int(userinput[6])*20)
b2 = str(int(userinput[4])*20)
b3 = str(int(userinput[2])*20)
b4 = str(int(userinput[0])*20)
y = int(b1[0])+int(b1[1])+int(b2[0])+int(b2[1])+int(b3[0])+int(b3[1])+int(b4[0])+int(b4[1])
x = (a+y)
if x % 10 == 0:
print('The account number you entered is valid!')
else:
print('The account number you entered is invalid!')
NOTE: If you are looking for cooked up code then please do not read the answer.
I can tell you the logic, I guess you are doing that wrong.
Logic:
Check whether the account number is 7 digit or not.
if condition 1 is true then Check if it is in the list of given accounts or not.
You can check condition 1 by checking is the condition 1000000<= user_input <10000000.
You can check the condition 2 by looping through the list.
You really have to go back and learn some of the basic syntax of python, because there are many problems with your code. Just google for python tutorials, or google specific issues, like how to get a user's input in python.
Anyway, I don't mean to insult, just to teach, so here's my solution to your problem (normally i wouldn't solve all of it, but clearly you made some effort):
current_accts = ['5679034', '8232322', '2134988', '6541234', '3984591', '1298345', '7849123', '8723217']
user_input = input("Please, enter the 7 digit account number: ")
if user_input in current_accts:
print('The account number you entered is valid!')
else:
print('The account number you entered is invalid!')
I didn't place it in a function that returns 0 or 1 like your question demanded, I suggest you adapt this code to make that happen yourself
Ok, can someone tell me how to make an if else statement with a character limit
i wrote a quick code to make you see what am trying to tell you
pass1 = input("What is you'r password: ")
pass2 = input("Rewrite you'r password: ")
what i tried:
if pass1 == <5:
print ("password is greater than 5")
else:
print ("password is lower than 5")
basically am trying to make a limit like in real website when you register (when you register there's a character limit example it cant be lower than 5 characters).
You need to test the length of the string with len function:
if len(pass1) < 5:
Perhaps you may have some confusion also with if statements and arithmetic operators. Check them here:
Control Flow
Comparissons