SyntaxError: invalid character in identifier in Python

Dung Do Tien Dec 26 2021 209

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I learn how to define and call a function, I created a function to help validate age, print validates age if age between 20 and 60. Like this:

 def valid_age(x):
   if x in range(20, 60):
     print("%s is valid age." % str(x))
   else :
     print("Your age is invalid.")

valid_age(5)

But I get an exception throw SyntaxError: invalid character in identifier;

 File "main.py", line 7
    valid_age(5)
               ^
SyntaxError: invalid character in identifier

I feel have some problems when I call valid_age method, but I don't know how to resolve it.

I am using python 3.9

Anyone can explain it to me? And how can I solve it?

Thanks for any suggestions.

Have 2 answer(s) found.
  • k

    khati Dec 26 2021

    I think you copy code from another website or your keyboard input has a problem. You can remove the line code error( line 7) and re-type it. It will work for you.

     def valid_age(x):
       if x in range(20, 60):
         print("%s is valid age." % str(x))
       else :
         print("Your age is invalid.")
    
    valid_age(20)

    It's sure works correctly for you.

  • k

    kezia johnpaul Dec 26 2021

    I try to copy your code and run and I got the same error. I try remote valid_age(5)line and re-type it to valid_age(5) and it worked for me.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close