Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Finding all non alpha-numeric characters in a string

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.

like image 324
thesoundandthefury Avatar asked Jul 20 '26 04:07

thesoundandthefury


2 Answers

You can simply try:

bonus = min(sum(not c.isalnum() for c in pwd), 3)
like image 94
arshajii Avatar answered Jul 21 '26 18:07

arshajii


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"))
like image 25
MxLDevs Avatar answered Jul 21 '26 19:07

MxLDevs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!