0

What I am trying to do

I have a simple function for string input validation.

Code:

def get_string_input(prompt):
    while True:
        try:
            value = str(input(prompt))
        except ValueError:
            time.sleep(0.3)
            print("Incorrect command. Please try again")
            continue

        if type(value) != str:
            time.sleep(0.3)
            print("Incorrect command. Please try again")
            continue
        else:
            break
    return value

# Test

get_string_input('What is your name? ')

Problem:

When I run the program, I recieve the following error:

enter image description here

I can see that my indentation is correct, so why is it giving me an error?

some_user_3
  • 403
  • 1
  • 5
  • 17
  • 1
    Is this the actual code? I ran it on my system and it worked fine. – Ismail Hafeez Mar 03 '21 at 10:10
  • interesting. i just copied it from my question here back into my program and it works fine for some reason... – some_user_3 Mar 03 '21 at 10:14
  • Perhaps there was some problem with the code but when you copied it over you accidently fixed it. Anyways use the code you posted here. – Ismail Hafeez Mar 03 '21 at 10:18
  • You had mixed tabs and spaces. Copying the question back in solved it for you... (probably used only spaces) – Tomerikoo Mar 03 '21 at 10:19
  • oh ok. i understand now. thanks for that. and yes the question you linked/your answer have answered my question :D – some_user_3 Mar 03 '21 at 10:20
  • In future, if the interpreter says you have an indentation error (or any other syntax error) bear in mind *that the interpreter is always right* in this regard, whatever you may think, because the interpreter is an operational definition of correctness. – BoarGules Mar 03 '21 at 13:23
  • ok :D thanks for that – some_user_3 Mar 03 '21 at 14:15

0 Answers0