0

I am a beginner python programmer and was hoping to get some help. I was trying to create a procedure where it would check if a user's input is either empty or a single alphabetic character. I am having trouble as every time I run it, and I type 2, it doesn't print anything. However, if I do multiples letters such as "ad", it says to enter only one character...

What I did:

# Create Validate function

def validate_input(LETTER):
      if len(LETTER) == 0 or len(LETTER) == 1:
        pass
      elif len(LETTER) >= 2 or not LETTER.islapha():
        print('Sorry, please enter a single letter')
      else:
        print('Sorry, please enter a letter')
#Ask for inputs

# Create function to validate input that returns true or false. If false then ask for input again.


first_char = input('Enter first character(lower cases) or press Enter: ')

validate_input(first_char)

What I got:

Enter first character(lower cases) or press Enter: 2
Enter second character(lower cases) or press Enter: ad
Sorry, please enter a single letter

Thanks in advance for your help...

1 Answers1

1

You need to change the order of your checks. At the moment, the check for the length happens before the check for the type of character, and a success on the first check will skip the later ones.

def validate_input(LETTER):
      if len(LETTER) == 0:
        pass
      elif len(LETTER) >= 2 or not LETTER.isalpha():
        print('Sorry, please enter a single letter')
      elif len(LETTER) == 1:
        pass
      else:
        print('Sorry, please enter a letter')
Dakeyras
  • 1,829
  • 5
  • 26
  • 33
  • @BuilderboiYT You're welcome :) If this fixed your problem, you can accept it as an answer by hitting the check mark – Dakeyras Nov 06 '22 at 22:02
  • 1
    @Dekeyras Thanks for the help. Sorry for another question. I actually want to allow the user to input an empty answer. When they would press enter, the program would immediately move on to question 2 and so on. How would I do that within the procedure? If unable to answer, it is alright. Thank You! – BuilderboiYT Nov 06 '22 at 22:07
  • @BuilderboiYT For that you would need to separate out one of the joint checks - see the new edited answer :) – Dakeyras Nov 06 '22 at 22:46