Im a begginner and basically what I want to do is, a program that reads a string and displays the appropriate messages, after checking wheter:
a.contains only letters
b.contains only uppercase letters
c.contains only lowercase letters
d.contains only digits
e.contains only letters and numbers
f.starts with a capital letter
g.ends with a dot
msg = input("Please enter your message: ")
if msg.isalpha() == True:
aux = "Your message contains only letters."
print(aux)
elif msg.isupper() == True:
print("Your message contains only uppercase letters.")
elif msg.islower() == True:
aux = "Your message contains only lowercase letters."
print(aux)
elif msg.isdigit() == True:
print("Your message contains only digits.")
elif msg.isalpha() and msg.isdecimal() == True:
print("Your message contains only letters and numbers.")
elif msg.capitalize() == True:
print("Your message begins with a capital letter.")
elif msg.endswith(".") == True:
print("Your message ends with a dot.")
That is my code but it only prints one or two of the variables I created. For example, when I type 'ABCD123' it prints that the message only contains uppercase letters, but I also want it to print that my message contains letters and numbers.
CodePudding user response:
Your code will only print one of the outputs because you are using 'elif'. From you question it seems like you would want to use individual if statements to avoid the conditional checks breaking once one is satisfied. such as:
if msg.isalpha() == True: aux = "Your message contains only letters." print(aux)
if msg.isupper() == True: print("Your message contains only uppercase letters.")
if msg.islower() == True: aux = "Your message contains only lowercase letters." print(aux)
CodePudding user response:
You should use if instead of elif. In this case, once you enter the first "if", the program stops and does not check the other conditions you have set with elif.