Python if statement: less than - python

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

Related

Trying to understand If statements when trying to incorporate user Input

I'm an absolute beginner - like I'm Today-years-old with this.
So I'm mucking about with a silly little piece of code to try and help me understand If statements:
print ('How many sausages have you eaten today?')
userInput = ('>5' or '<5')
if userInput input == int ('> 5'):
print ('Whoa! Slow down Fatty!')
elif userInput input == ('< 5'):
print ('Ok, but better call it a day now')
I'm trying to alter the printed message based on how many sausages the user inputs - e.g. above 5 or below 5.
I know that I'm doing something (probably many things) wrong.
Can anyone help to tidy this up?
Here is a fixed version:
There is some notes to help you understand
# To get an input you need to use input()
userInput = input('How many sausages have you eaten today?')
# Turn the input from text to a number with int()
userInput = int(userInput)
if userInput > 5:
print ('Whoa! Slow down Fatty!')
elif userInput < 5:
print ('Ok, but better call it a day now')
Your code has a few problems. First, you cannot use int() to get user input. Instead, you must use input(). Second, you cannot convert inequalities into integers. Like this:
print('How many sausages have you eaten today?')
userInput = int(input())
if userInput > 5:
print('Whoa! Slow down Fatty!')
else:
print('Ok, but better call it a day now')
Notice how I get user input with input(), which will return a string. Then, I convert it to an integer with int(). If the user inputs something that is not an integer, then the program will crash because the input to int() must be able to be converted into an integer.
In the if-else statement, I check if userInput, which is an integer, is greater than 5. I also used an else statement, not an elif, because if userInput is exactly 5 then neither statement would have been true.

Comparing raw input to an specific element in an array using Python

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

Password checker error - python

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!

Program in Python that will prompt the user to enter an account number consists of 7 digits?

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

password strength code help python

so i am writing code in python to tell you your password strength but it is not doing as i say and keeps saying invalid password password unless you use only numbers
hy=1
while(y==1):
passwordentered=str(input("plaese enter your proposed password "))
x=len(passwordentered)
numbers=passwordentered.count ("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")
lowerletters=passwordentered.count ("a"and"b"and"c"and"d"and"e"and"f"and"g"and"h"and"i"and"j"and"k"and"l"and"m"and"n"and"o"and"p"and"q"and"r"and"s"and"t"and"u"and"v"and"w"and"x"and"z")
higherletters=passwordentered.count ("A"and"B"and"C"and"D"and"E"and"F"and"G"and"H"and"I"and"J"and"K"and"L"and"M"and"N"and"O"and"P"and"Q"and"R"and"S"and"T"and"U"and"V"and"W"and"X"and"Z")
if(numbers>0 and lowerletters==0 and higherletters==0):
david=9
elif(lowerletters>0 and numbers==0 and higherletters==0):
david=9
elif(higherletters>0 and numbers==0 and lowerletters==0):
david=9
elif(higherletters>0 and numbers>0 and lowerletters==0):
david=8
elif(higherletters>0 and lowerletters>0 and numbers==0):
david=8
elif(numbers>0 and lowerletters>0 and higherletters==0):
david=8
elif(numbers>0 and lowerletters>0 and higherletters>0):
david=7
elif(x>12 or x<6):
david=10
elif(lowerletters==0 and numbers==0 and higherletters==0):
david=10
if(david==10):
print("the password you entered was invalid\
why not try again.")
y=1
elif(david==9):
print("the password you entered is very weak,try to include numbers, lower case letters and upper case letters. why not have another go.")
y=1
elif(david==8):
print("your password is good but it could be better try to include numbers, lower case letters and upper case letters. why not have another go.")
y=1
elif(david==7):
print("your password is really good. thank you for using this program")
y=0
any help would be appriciated
First of all, Python is a very forgiving language, you can get away with a lot of stuff that you can't do in other languages, HOWEVER:
numbers=passwordentered.count("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")
Will not work. Python will evaluate everything within the brackets as a boolean expression and this line of code essentially boils down to:
numbers=passwordentered.count("9")
The same goes for the other checks as well. To get what you actually wanted from this, you should use a loop of some sort. You can use something like this:
numbers = sum( ch.isdigit() for ch in passwordentered )
This will give you a count of how many digits are found within the password string. You can modify this to count the number of lowercase and uppercase letters too.
Finally, you should try to clean up your if statements, you could break them down a bit more neatly and make this more readable.
Those count functions won't work if written like that.
Try to replace this:
numbers=passwordentered.count ("1"and"2"and"3"and"4"and"5"and"6"and"7"and"8"and"9")
lowerletters=passwordentered.count ("a"and"b"and"c"and"d"and"e"and"f"and"g"and"h"and"i"and"j"and"k"and"l"and"m"and"n"and"o"and"p"and"q"and"r"and"s"and"t"and"u"and"v"and"w"and"x"and"z")
higherletters=passwordentered.count ("A"and"B"and"C"and"D"and"E"and"F"and"G"and"H"and"I"and"J"and"K"and"L"and"M"and"N"and"O"and"P"and"Q"and"R"and"S"and"T"and"U"and"V"and"W"and"X"and"Z")
with the following:
numbers = sum(typ in "0123456789" for typ in passwordentered)
lowerletters = sum(typ in "abcdefghijklmnopqrstuvwxyz" for typ in passwordentered)
higherletters = sum(typ in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for typ in passwordentered)
or the following for a verbose approach:
numberlist = "0123456789"
Lletterlist = "abcdefghijklmnopqrstuvwxyz"
Hletterlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers=0
lowerletters=0
higherletters=0
for f in xrange(0, x):
if numberlist.find(passwordentered[f]) != -1:
numbers = numbers+1
if Lletterlist.find(passwordentered[f]) != -1:
lowerletters = lowerletters+1
if Hletterlist.find(passwordentered[f]) != -1:
higherletters = higherletters+1

Categories

Resources