Home > Mobile >  If statement within for statement is only outputting the print statement of the else portion even th
If statement within for statement is only outputting the print statement of the else portion even th

Time:03-29

I am trying to get an if/else statement to run in a for loop. Despite the argument meeting the criteria for all statements above it, the output is the else portion. Here is my code:

import string

print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.ascii_letters)
print(string.digits)
print(string.punctuation)

my_string = '4n%$P9'
  
for index in range(0, len(my_string), 1):
  if my_string in string.ascii_lowercase:
    print("'{}' is a lowercase letter.".format(my_string))
  elif my_string in string.ascii_uppercase:
    print("'{}' is an uppercase letter.".format(my_string))
  elif my_string in string.digits: 
    print("'{}' is a digit in arabic.".format(my_string))
  elif my_string in string.punctuation:
    print("'{}' is a punctuation mark.".format(my_string))
  else:
    print("Does not compute.")

The output is this:

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
!"#$%&'()* ,-./:;<=>?@[\]^_`{|}~
Does not compute.
Does not compute.
Does not compute.
Does not compute.
Does not compute.
Does not compute.

I'm not getting a syntax error so I am wondering, am I not using the correct keywords? Is the logic out of order?

CodePudding user response:

The problem isn't with the if statements so much as your for loop. It looks like you want to iterate over each character in my_string and test it separately, not test my_string all at once:

for c in my_string:
    if c in string.ascii_lowercase:
        print(f"'{c}' is a lowercase letter.")
    elif c in string.ascii_uppercase:
        print(f"'{c}' is an uppercase letter.")
    elif c in string.digits: 
        print(f"'{c}' is a digit in arabic.")
    elif c in string.punctuation:
        print(f"'{c}' is a punctuation mark.")
    else:
        print(f"'{c}' does not compute.")

prints:

'4' is a digit in arabic.
'n' is a lowercase letter.
'%' is a punctuation mark.
'$' is a punctuation mark.
'P' is an uppercase letter.
'9' is a digit in arabic.

Note that printing c as part of the "does not compute" message allows you to see what character didn't compute -- if you had done this with my_string in your original code, you would have seen:

'4n%$P9' does not compute.
'4n%$P9' does not compute.
'4n%$P9' does not compute.
'4n%$P9' does not compute.
'4n%$P9' does not compute.
'4n%$P9' does not compute.

which would have been a good clue as to what the problem was!

CodePudding user response:

You are checking whether the entire string is a substring of each of the constants from the string module. You want to perform that check for each character in the string, like so:

import string

my_string = '4n%$P9'
  
for char in my_string:
  if char in string.ascii_lowercase:
    print("'{}' is a lowercase letter.".format(char))
  elif char in string.ascii_uppercase:
    print("'{}' is an uppercase letter.".format(char))
  elif char in string.digits: 
    print("'{}' is a digit in arabic.".format(char))
  elif char in string.punctuation:
    print("'{}' is a punctuation mark.".format(char))
  else:
    print("Does not compute.")

This outputs:

'4' is a digit in arabic.
'n' is a lowercase letter.
'%' is a punctuation mark.
'$' is a punctuation mark.
'P' is an uppercase letter.
'9' is a digit in arabic.

CodePudding user response:

You are trying to compare if an entire string is in string.ascii_lowercase, not just the letters, you need to actually run my_string[index].

import string

print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.ascii_letters)
print(string.digits)
print(string.punctuation)

my_string = '4n%$P9'
  
for index in range(0, len(my_string), 1):
  if my_string[index] in string.ascii_lowercase:
    print("'{}' is a lowercase letter.".format(my_string[index]))
  elif my_string[index] in string.ascii_uppercase:
    print("'{}' is an uppercase letter.".format(my_string[index]))
  elif my_string[index] in string.digits: 
    print("'{}' is a digit in arabic.".format(my_string[index]))
  elif my_string[index] in string.punctuation:
    print("'{}' is a punctuation mark.".format(my_string[index]))
  else:
    print("Does not compute.")
  • Related