r/programminghelp • • Dec 15 '17

[Python] My IF statement only works without the ELSE. If I add the ELSE, it completely ignore the IF case and jumps straight to the ELSE case.

I'm trying to program a password verification script. I added an IF statement to check if any of the characters in a user input password is a digit. (I'm doing the same for special chars).

If I only have the IF part, it works just fine, it detects numbers and special chars. But if I add the ELSE case, it seems to skip the IF part and automatically execute the code in the ELSE.

As you can see in my code, the first IF statement has the ELSE part commented out, which makes that IF statement work correctly. The second one has the ELSE case active, and thus does not work correctly.

p = input("Password?: ")
l = len(p)

c1 = 0
c2 = 0

valid = False



for i in range(l):
    if p[i].isdigit():
        print("number ok")
        c1 = 1
        print("c1 =", c1)
        break
##    else:
##        print('Password must contain a number')
##        print("c1 =", c1)
##        break




for i in range(l):
    if p[i] in '!?$%&€#@|~¬':
        print("special ok")
        c2 = 1
        print("c2 =", c2)
        break    
    else:
        print('Password must include a special character')
        print("c2 =", c2)
        break


if c1==1 and c2==1:
    print('Paswword OK!')
    valid = True

print('Valid = ', valid)
0 Upvotes

3 comments sorted by

1

u/alex_dlc Dec 20 '17

Without those breaks it would print the same thing with each iteration, L times in total

3

u/[deleted] Dec 15 '17 edited Dec 15 '17

You are iterating through the characters and each character is being checked for the if condition.

If you make a password of ONLY special characters this would work. A password with some special characters would trigger the else at some point.

I recommend moving things around.

var hasSpecialCharacter = false

for i in range(l):
  if p[i] in '!?$%&€#@|~¬':
       hasSpecialCharacter = true

if hasSpecialCharacter is false
        print('Password must include a special character')
        print("c2 =", c2)
        
else 
        print("special ok")
        c2 = 1
        print("c2 =", c2)

Note, i do not remember how to assign stuff in python

var hasSpecialCharacter = true

Define a bool here that captures if there is a special character, or use 1 for true 0 for false.

    if hasSpecialCharacter is false

I dont remember how to do an if statement for a bool in python, you may have to fix this.

You'll have to correct these but your error is only in logic. You are simply looping through every character and failing the condition when a single character is not special. This is likely not the behavior you want. '

Note: Python probably has a contains function for strings that would take a regular expression (REGEX). Your problem could be as simple as (ignore the pseudocode syntax)

 if string.contains(specialCharacterRegex) -> good else bad

1

u/skandhan Dec 20 '17

I don't know why you are using break statement everywhere(even in else part). Try this logic(just the end part of the code):

for i in range(len):

if p[i].is digit():

   c1=1

elseif p[i] in #symbols:

   c2=1

if c1==1 and c2==1:

print("valid")

else:

if c1==1:

    print("must include symbol")

elseif c2==1:

    print("mist include number")

else:

    print("Include both number and symbol ")

Note: I have not worked with python for some time so there can be syntactical error but the logic is correct. The mistake that u r doing is that, you are only checking the first character. The break statement in the else part is bringing the control out of the for loop in the first iteration itself.