Too Much Wait TIme

Question:

So I was trying to use a procedure(with a parameter)…I asked the user for inputs and created a validate function to check the inputs and see if they are strings…I checked it but the outputs are taking too long to output. How do I fix this?

I tried:

# Create Validate function

def validate_input(LETTER):
  while True:
    try:
      if len(LETTER) == 0:
        pass
    except:
      if len(LETTER) >= 2:
        print('Sorry, please enter a single letter')
      if LETTER.strip().isdigit():
        print('Sorry, please enter a letter')
        break

#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)

second_char = input('Enter second character(lower cases) or press Enter: ')

validate_input(second_char)

third_char = input('Enter third character(lower cases) or press Enter: ')

validate_input(third_char)

fourth_char = input('Enter fourth character(lower cases) or press Enter: ')

validate_input(fourth_char)

fifth_char = input('Enter fifth character(lower cases) or press Enter: ')

validate_input(fifth_char)

But it came out to be:

Enter first character(lower cases) or press Enter: 2

And from there it takes too much time to say it it must be a string…

Thank you in advance!

Asked By: BuilderboiYT

||

Answers:

the input() function in python always take the inputs as string, if you want to get integer as input then the following functions would be used => int(input())

it is not taking too long to execute it just a logical error in your code 😉

lets say the the input is "hello" so the len("hello") is 5

now , the input goes to the validation function(your function) first it starts with infinite while loop with no terminal condition and starts to checks the condition len(LETTER) == 0 whether it is true or false but it won’t raise any exception so it won’t go to the except block where the actual terminal condition is located(break) so its keep running forever.

hope it’ll be helpful for you, thank you.

Answered By: Hariharan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.