Reputation: 5
words = input("Enter a Letter: ")
int_words = int(words)
if words.isupper() == words.islower():
print("Contains only letters")
elif words.isupper() > words.islower():
print("Contains only upper letters")
else:
print("Contains only lower letters")
if int_words.isdigit():
print("Contains only digits")
else:
print("Contains Letters and Digits")
"AttributeError: 'int' object has no attribute 'isdigit'" I just want to convert the string into int so that I could only run the print("Contains only Digits")
I really need help. I'm a freshmen in coding so I'm really new into this. :(
Upvotes: 0
Views: 3068
Reputation: 306
In the below code sample I first evaluated if the string (words) was a digit, if it wasn't then I proceed to check if the string contained a digit by using a for loop, you can read more about for loops in [here][1] and [here][2] for the rest of the script I just evaluated if the any of the other conditions where met by using is and is not comparison statements, you can read more about it in [here][3] and then proceeded to print the desired output.
words = input("Enter a Letter: ")
numbers = False
if not words.isdigit():
for character in words:
if character.isdigit():
print("Contains Letters and Digits.")
numbers = True
break
if not words.isdigit() and words.islower() and not words.endswith(".") and not numbers:
print("Contains only letters.")
print("Contains only lower case letters.")
elif not words.isdigit() and words.islower() and words.endswith(".") and not numbers:
print("Contains only letters.")
print("Contains only lower case letters.")
print("Ends with period.")
elif not words.isdigit() and words.isupper() and not words.endswith(".") and not numbers:
print("Contains only letters.")
print("Contains only upper letters.")
elif not words.isdigit() and words.isupper() and words.endswith(".") and not numbers:
print("Contains only letters.")
print("Contains only upper letters.")
print("Ends with period.")
elif words.isdigit():
print("Contains only digits.")```
[1]: https://realpython.com/python-for-loop/
[2]: https://www.w3schools.com/python/python_for_loops.asp
[3]: https://realpython.com/python-is-identity-vs-equality/
Upvotes: 0
Reputation: 2379
This solve this the problem:
words = input("Enter a Letter: ")
if words.isalpha():
print("Contains only letters")
elif words.isupper():
print("Contains only upper letters")
elif words.islower():
print("Contains only lower letters")
elif words.isalnum():
print("Contains only digits")
Upvotes: 0
Reputation: 11342
This should help get you started:
lstupper = [chr(x) for x in range(65,90+1)] # A-Z
lstlower = [chr(x) for x in range(97,122+1)] # a-z
lstnums = ['0','1','2','3','4','5','6','7','8','9']
lstletters = lstupper + lstlower
lstlettersandnums = lstupper + lstlower + lstnums
word = 'apple'
allletters = True
allnums = True
for c in word: # each letter in 'apple'
if not c in lstnums: # if character not in number list
allnums = False
if not c in lstletters: # if character not in letter list
allletters = False
print(allnums, allletters) # False True
Upvotes: 0