Legality identifier | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Legality identifier

Hello, I have a Python Assignment, and I have to find the identifier legality and justification. However, I only have learned about data type and the teacher is not tough about justification and legal. I learned that on YouTube, my problem is how to write that in Python and execute that. I got confused not finding a way to write the code. Ex: identifier = "Boys and Girls" is_legal = identifier.identifier() if is_legal: print(f"{identifier} is a legal identifier.") else: print(f"{identifier} is not a legal identifier.") This example is too advanced because the teacher did not teach this yet. I have to use simple commands. Could anyone give me a guide?

24th Jan 2024, 2:42 AM
Robin Marma
Robin Marma - avatar
22 Answers
+ 1
no function version and no iskeyword checking:(assuming importing modules is also not yet taught) #put all your identifiers in a tuple test = ('5boys', 'Boys and Girls', 'number_of_birds', 'None', 'or', 'and', '-continue') #iterate through the tuple and print the result. for s in test: if s.isidentifier(): print(s, '(L) (R) (useful)') else: print(s, '(I) (NR) (not useful)')
24th Jan 2024, 8:56 AM
Bob_Li
Bob_Li - avatar
+ 5
Maybe you mean the isidentifier() method of the string. https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_isidentifier.asp This function will tell you if you are allowed to use the text as a name in your code, for example as a variable name or function name. An identifier is valid, if it only contains letter, numbers and underscores, and it cannot begin with number.
24th Jan 2024, 2:58 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Tibor Santa interesting. Python's string methods is quite comprehensive. cases where it fails is Python keywords: None, and, or, if... 'None'.isidentifier() returns True but None = 500 returns an error.😅 While reading help(str.isidentifier), there is also a reference to keyword.iskeyword(s). This is used to check if s is a built-in Python keyword. you have to add import keyword to use it. Perhaps it might be useful if you want to check if a string is already a Python keyword use it as part of the validity check?Of course, It depends on your teacher's requirements.
24th Jan 2024, 4:35 AM
Bob_Li
Bob_Li - avatar
+ 1
can you post your actual code with the actual given identifier you are testing? It is hard to tell from these bits and pieces of information. from the code you gave, you wrote: is_legal = identifier.identifier() maybe it should be is_legal = identifier.isidentifier() as Tibor Santa suggested?
24th Jan 2024, 4:53 AM
Bob_Li
Bob_Li - avatar
+ 1
other than you can't start a variable name with '-', so -continue should be _continue, The rest of the code does not give me any idea on what needs to be done, the input parameters and the expected output... Perhaps others here can make more sense of this. Yeah, it's confusing alright...
24th Jan 2024, 5:43 AM
Bob_Li
Bob_Li - avatar
+ 1
maybe something like this? I added an iskeyword check because keywords also can't be used as variable names. import keyword as kw def validator(s): if s.isidentifier() and not kw.iskeyword(s): return '(L) (R) (useful)' return '(I) (NR) (not useful)' test = ('5boys', 'Boys and Girls', 'number_of_birds', 'None', 'or', 'and', '-continue') for i in test: print(i, validator(i))
24th Jan 2024, 6:44 AM
Bob_Li
Bob_Li - avatar
+ 1
Robin Marma , This is not a string, so you won't be able to test it. -continue This is a string, so you will be able to test it. "-continue" print("-continue".isidentifier()) # False
24th Jan 2024, 7:02 AM
Rain
Rain - avatar
+ 1
Bob_Li, def and if statement, not taught yet. So advanced command prompts are not allowed yet. The teacher mentioned in this practical Assignment we will learn How to read different objects through the keyboard How to write meaningful user prompts How to write meaningful output How to use Python reference variables. He also provided data-type examples (int, float, bool) Could this be the input function related to this question? Anyway, thanks for your time, I'll try to find a solution for this.
24th Jan 2024, 8:14 AM
Robin Marma
Robin Marma - avatar
+ 1
Bob_Li, your example is beautiful and readable thanks for that. I'll use that way to evaluate, I hope the teacher expectations are different🤣🤣🤣
24th Jan 2024, 9:08 AM
Robin Marma
Robin Marma - avatar
+ 1
Bob_Li, what do you mean by evaluating, i don't know the right word to say that why I used evaluate.
24th Jan 2024, 9:13 AM
Robin Marma
Robin Marma - avatar
0
The identifier is already given. first one is this,-> (-continue) and I asked from senior he said use the identifier variable and put the Legal or not inside the parentheses and justify the variable, But it gets the error.
24th Jan 2024, 4:48 AM
Robin Marma
Robin Marma - avatar
0
https://sololearn.com/compiler-playground/cG1WGWqA43HD/?ref=app The teacher hasn't taught us that much we are still getting basic command, so i can't use that command yet.
24th Jan 2024, 5:29 AM
Robin Marma
Robin Marma - avatar
0
Maybe I am defining the question the wrong way. -continue i want this identifier output as illegal and for justification not useful 5boys Also illegal i know that but i want to print this as illegal. I am also confusing you guys😅😅 sorry for that. There is a valid identifier given number_of_birds=10 could you show me how to evaluate in Python this is legal so I need to put inside parentheses to refer to legal as a (L)
24th Jan 2024, 6:06 AM
Robin Marma
Robin Marma - avatar
0
Rain, I tried this too, but I don't want the output as a bool type. I have to assign this value into a variable (i think) (legal(L) illegal(i) not recommended (NR \R)). Variable already given -continue this variable is the first one. Anyway thanks to you all.
24th Jan 2024, 8:27 AM
Robin Marma
Robin Marma - avatar
0
ok, so perhaps you just need to concentrate on what are the condtions needed to write a valid python identifier. A valid identifier can have letters (uppercase and/or lowercase), digits and underscores. The first letter of an identifier should be either a letter or an underscore. You cannot use keywords like int , while etc. as identifiers.
24th Jan 2024, 8:36 AM
Bob_Li
Bob_Li - avatar
0
Well I create a dictionary it should right. https://sololearn.com/compiler-playground/cQVw7FXuiFuv/?ref=app
24th Jan 2024, 8:51 AM
Robin Marma
Robin Marma - avatar
0
Robin Marma you might be printing the correct answers, but YOU are doing the evaluating, not the program itself. It is essentially one big fancy print statement. what if new words are added? You would have to manually update the dictionary. This defeats the purpose of writing a program. Also, evaluating whether an identifier is meaningful or not is a far more complex task than your teacher might expect...
24th Jan 2024, 9:08 AM
Bob_Li
Bob_Li - avatar
0
evaluating, or making the assignment or decision on what value to give to the key in your dictionary. If you need to write it yourself, then the program is just simply printing, not helping you make the decision.
24th Jan 2024, 9:16 AM
Bob_Li
Bob_Li - avatar
0
Robin Marma have the teacher taught you about imports? Because without the keyword module, the results will not be able to detect Python's keywords and it will be wrong for some cases. the better version will be: import keyword as kw # put all the identifiers you want to test in this tuple test = ('5boys', 'Boys and Girls', 'number_of_birds', 'None', 'or', 'and', '-continue') #test the identifiers one by one for s in test: if s.isidentifier() and not kw.iskeyword(s): print(s, '(L) (R) (useful)') else: print(s, '(I) (NR) (not useful)')
24th Jan 2024, 9:25 AM
Bob_Li
Bob_Li - avatar
0
Bob_Li, I didn't understand your question, 😅😅language barrier causes the problem for me.
24th Jan 2024, 9:26 AM
Robin Marma
Robin Marma - avatar