Home > Software design >  Problem with my program for getting only letters (upper and lower case)
Problem with my program for getting only letters (upper and lower case)

Time:10-28

I'm trying to write a script which asks a user for input and validates the input in a loop, but I'm getting the error: TypeError: 'str' object cannot be interpreted as an integer.

I've tried a while and a for loop. Even without loop and just with an if and an else.

Here's what I have so far:

def majuscule(word):
    for j in word:
        if (chr(j) >=  chr(0)) and (chr(j) <= chr(64)) and (chr(j) >= chr(91)) and (chr(j) <= chr(96)) and (chr(j) >= chr(123)):
           word = input("What's your word ?")
        else:
            print('Valide word')

CodePudding user response:

To check if a word is just letters or not, use the in-built python function of isalpha()

def majuscule(word):

    if word.isalpha():
          
        print('Valide word')

    else:
        
        word = input("What's your word ?")

Remember that isalpha() only returns True for alphabets, even if there is a 'space', it returns False

CodePudding user response:

You can use functions islower() and isupper() per symbol. You did not provide examples to your problem so I don't understand exactly what you're looking for but telling if symbol is upper per char you can use something like this

def majuscule(word):
    for symbol in word:
        if symbol.islower():
           print(f"{symbol} is lower cased")
        elif symbol.isupper():
           print(f"{symbol} is upper cased"
        else:
            print("Symbol {symbol} is neither upper cased nor lower cased")
  • Related