Python 3- password check [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the problem: write a function that checks whether a string is valid password.
Rules: Must have at least 8 characters A password must consist of only letters and digits A password must contain at least 2 digits
I am having trouble with def CountsDigitsFor, I want it to check for at least 2 digits.
def getPassword():
return input("Enter password: ")
def CountDigitsFor(password):
res = []
for i in password:
if i.isdigit():
res.append(i)
return int
def validPassword(password):
if len(password) >= 8:
if password.isalnum():
if CountDigitsFor(password) >= 2:
return True
else:
return False
def main():
password = getPassword()
if validPassword(password):
print(password + " is valid")
else:
print(password + " is invalid")
main()

return int
is just returning the class int. What you want to return is the number of digits. That of course would be:
return len(res)

Why not just use this one:
def CountDigitsFor(password):
return sum(character.isdigit() for character in password)
I have provided the above in your other question, but just in case that wasn't clear I am going to give you a brief description on how this works.
The above is equivalent to what you want but in one single line. The idea is that you loop through the characters of the password, check if the character is a number and then get the sum of the digits in the password.
Then after you call the CountDigitsFor() you will get the number of digits in the password and you can check if they are at least 2 with the if statement you have provided.
if CountDigitsFor(password) >= 2:
return True

Related

Would anyone know the code in python that prints ONLY if it's a binary string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
The real problem is there:
write a program that takes a string as input and prints “Binary Number” if the string contains only 0s or 1s. Otherwise, print “Not a Binary Number”.
You can iterate every character of the input and verify it is a zero or a one. The all function can be used for this iteration and check.
Also require that the string has at least one character:
def isBinary(string):
return all(ch in "01" for ch in string) and string != ''
Example use:
string = input("Enter a binary number: ")
if isBinary(string):
print("Thank you!")
else:
print("That is not a binary number.")
It is easy.
def is_binary(string_to_check:str):
for x in string_to_check: #loop though all letter
if x in ('0','1'):
continue #yep the letter is binary thingy
else:
return False #no
return True #yep the string is binary
Next time check google before ask.
def check_if_binary(string) :
p = set(string)
s = {'0', '1'}
if s == p or p == {'0'} or p == {'1'}:
print("Binary Number")
else :
print("Not a Binary Number")
string = str(input ("Enter the sting : "))
check_if_binary(string)
References: https://www.geeksforgeeks.org/python-check-if-a-given-string-is-binary-string-or-not/

How can I make return boolean value not printed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
return (True)
print (f'{word_1} is a palindrome')
elif word_1 != word_reversed:
return(False)
print(f'{word_1} is not a palindrome')
print(check_palindrome(word_1))
> `Here is the output:
Enter a string: level
level is a palindrome
True`
I want to write retun true value but make it unvisible. I should run it with the return
This?
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
print (f'{word_1} is a palindrome')
return (True)
elif word_1 != word_reversed:
print(f'{word_1} is not a palindrome')
return(False)
check_palindrome(word_1)
Instead of
print(check_palindrome(word_1))
You should just write
check_palindrome(word_1)
It will still print the line you expect since the function says to print it, but if you call the function in a print statement, it will always print the return value.
If you use return you assign a value to the function, printing that function first runs the function, and then prints it.
To solve this you could write : return str(word1, "is a palindrome") instead of returning True and False

How to limit the number of digits input? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am requesting someone to tell me a program that asks a user to input only 9 digits. Number 0 not at start but having 9 digits.
How can I limit the number of digits?
For example:
Num=int(input('please Enter a Number'))
if num?!
Please help me soon.
If i got this right this should work:
a=input()
if len(a)==9 and a[0] != '0' and a.isdigit():
#if you want it to be integer
a=int(a)
do something
Hope it helps.
Well if I understood correctly you need a program that asks the user to enter exactly 9 digits. In that case it's pretty simple.
i = input("Enter a nine digit number: ")
if len(i) != 9:
print("You must enter 9 digits!")
quit()
if not i.isdigit():
print("Your input must be a number!")
quit()
# num is a 9 digit integer
# i is a 9 digit string
num = int(i)
You can do this.
num = input('Please enter a number')
if not num[0] == "0" and len(num) == 9 and num.isdigit():
# You can print a message or cast the num into an integer here
# like this: input_number = int(num)
print(True)
else:
print(False)
So what's happening here is that the program will accept the user input and evaluate if the first digit is NOT 0 AND that the length is exactly 9 characters.

Print distinct numbers in python 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following code to print the (distinct numbers), but I'm trying to check the inputs before the process, should be ten digits numbers only with only one space.
I tried while and try but still can't figure out:
def main():
list1 = input("Enter ten numbers: ").split()
set1 = set(list1)
print(set1)
list2 = list(set1)
string = string.join(list2)
print("The distinct numbers are: " + str(string))
main()
A simpler version using the isnumeric() method built in strings. Also, I put a loop around the input so as the user can easily retry in case of wrong data:
def main():
while(True):
numbers = input("Enter ten space-separated numbers: ").split()
if len(numbers) != 10 or not all(n.isnumeric() for n in numbers):
print("Wrong input, please retry")
continue
numbers = ' '.join(set(numbers))
print("The distinct numbers are:", numbers)
break # or return numbers if you need the data
main()
Do you mean check if there are ten numbers inputted?
import re
def check_input(input_string):
compare_to = '\s'.join(10*['\d+'])
if re.match(compare_to[:-1], input_string):
return True
return False
This is a regular expression, it checks if the input string is equal to a specified input format. This one specifically checks whether 10 sets of at least 1 number [\d+] are added with a space inbetween \s.

Python functions issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wrote two functions. First one, when called checks if the number is three-digit, and second one is checking if the number is even or odd:
def even(n):
if n%2==0:
print('number {} is even'.format(n))
else:
print('number {} is not even'.format(n))
return
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return True
else:
print('I said three-digit number!! ::')
return False
main(0)
What my problem is, when I call function even like even(a), it gives me an error saying a is not defined.
You must return a in the main() function and pass it to the even function.
def main(n):
kraj=True
while(kraj):
a=int(input('three-digit number pls: '))
if (a>99 and a<1000):
kraj=False
return a
else:
print('I said three-digit number!! ::')
return False
a = main(0)
even(a)
You have to return the value of a so you can use it somewhere else
Also you have a while and return in if and else.
Therefore you do not need your while loop. Or you want the user to reenter on error you then have to get rid of the return in else
You should give your function a more meaningfull name like read_threedigit_number
You normally don't put paranthesis around if and while
Improved Code
def read_threedigit_number(n):
while True:
a=int(input('three-digit number pls: '))
if a>99 and a<1000:
return a
else:
print('I said three-digit number!')
a = read_threedigit_number(0)
even(a)

Categories