Python - Finding all non alpha-numeric characters in a string
17,118
Solution 1
You can simply try:
bonus = min(sum(not c.isalnum() for c in pwd), 3)
Solution 2
If you want to count the number of non-alpha strings you could say
def strength(string):
'''Computes and returns the strength of the string'''
count = 0
# check each character in the string
for char in string:
# increment by 1 if it's non-alphanumeric
if not char.isalpha():
count += 1
# Take whichever is smaller
return min(3, count)
print (strength("test123"))
Author by
thesoundandthefury
Updated on June 15, 2022Comments
-
thesoundandthefury almost 2 years
I'm designing a system that allows users to input a string, and the strength of the string to be determined by the amount of non alphanumeric characters. Points should be awarded like so: +1 for every non-alnum character to a maximum of 3 non-alnum characters.
def non_alnum_2(total,pwd): count = 0 lid = 3 number = 0 if pwd[count].isalnum(): if True: print "Nope" if False: print "Good job" count = count + 1 number += 1 if number > lid: number = lid return number total = 0 number = 0 pwd = raw_input("What is your password? ") non_alnum_2(total, pwd) print total total += number
I've only just started coding, so I'm sorry if this seems like a very junior question.