# A small python identifier generator for users

My teacher gave us this problem to solve so basically N is the number of user names in a list T with 1>N<9

• Generate another list called TID containing the name of the user but changed a bit. Do the next steps for each user in the list T

1. Take the first two letters from the user's name
2. The index of the user name in the list T
3. the ASCII code for the first user letter and the sum of vowels in the user name if the sum is > 90 we automatically go back to the letter a

Example: T = ("RAOUF".....) TID= ("RA0U")

• The first two letters are "RA"
• the index of the item in the list is 0
• The letter "R" its ascii code is 82 with the string containing three vowels it becomes 85 so we put the letter "U"

I apologize if there is any problem with my English, we studied all of this in french I had to translate it all. I appreciate any kind of help, thank you.

Here is my code/

n = int(input("Entrezy les numbers de les utilisateurs (1-9): "))
t = []
tid = []

def remplir_t(n, t):
while True:
if n < 1 or n > 9:
n = int(input("N doix etre un valeur de 1 a 9, rentrezy N:  "))
else:
break
for i in range(n):
Indentifacteur = input(
F"Entrezy le indentifacteur de le utilisateur {i + 1}: ")
t.append(Indentifacteur)

def voyel_ascii(ch):
v = 0
b = ord(ch[0])
voyells = ("A", "E", "I", "O", "U", "Y", "a", "e", "i", "o", "u", "y")
for i in range(len(ch)):
if ch[i] in voyells:
v += 1
b += v
if b > 90:
b = 97
k = chr(b)
return k

def remplir_tid(n, t):
for i in range(n):
tid.append(t[i][0:2]+str(i)+voyel_ascii(t[i]))
print(tid)

remplir_t(n, t)
remplir_tid(n, t)
print(t)


• I doubt that 1>N<9 - what are the constraints that your teacher gave you? Commented Oct 23, 2021 at 20:16

### The English language is the lingua franca of computing

I am not from an English speaking country, yet every piece of code I write is in English. Why? Because code is about communication, and being understood. The most important part of a piece of code is that it is transparent, or easy to digest and of course that it works. But how can you know that it works if you can not read it?

### PEP 8

Use a common standard for your variables and naming, for the most part it is good but Indentifacteur should be indentifacteur, or as mentioned above identifier.

### Meaningful Identifier Names

Giving identifiers in computer programs meaningful names is a widely accepted best practice. This is reflected both in coding standards and in programming teaching materials. For example, the Google C++ Style Guide states

"Give as descriptive a name as possible, within reason. Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader. Do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word."

### Negation of expressions

This bit here is a bit cumbersome

while True:
if n < 1 or n > 9:
n = int(input("N doix etre un valeur de 1 a 9, rentrezy N:  "))
else:
break


It would be better stated as N should be a value of 1 to 9, return N:

while True:
if 1 <= n <= 9:
break
n = int(input("N should be a value of 1 to 9, return N:"))


If you are using python 3.8 or above it could also be stated as

username_msg = "Input the number of usernames [0, 9] inclusive: "
pass


Again it would be better to add some error checking that the input truly is an integer for instance. However, this will do for now.

### Use builtins

for i in range(len(ch)):
if ch[i] in voyells:
v += 1


This can be rewritten as

for char in ch:
if char in voyells:
v += 1


It can also be completely rewritten see bellow.

### Final bits and bobs

I got tired from going through the code line by line so I did a complete rewrite.

• Everything is now in English
• There is no longer any global t or tid floating around. Messing with global state makes the code hard to read.
• Remover every piece of single letter variables and just did a complete refactor.
• More logical names for our function, the encoding to create the identifier is often called a transposition.
• Global constants should be in UPPERCASE
• voyel_ascii was split into two functions and much reduced.
• Typing hints added to hint at what is returned and inputed into each function.
• An if __name__ == "__main__" guard was included so this code can be safely imported elsewhere.

#### Code

from typing import Annotated

VOWELS = ["a", "e", "i", "o", "u", "y"]

Identifier = Annotated[str, "A letter identifying a particular username"]

def vowel_counter(string: str) -> int:
return len([char for char in string if char in VOWELS])

pass
return [
]

b = 97
return chr(b)

if __name__ == "__main__":

]

• I agree with you're saying, English should be used for programming except for the user prompts and interactive messages because frankly doing otherwise may not be acceptable in some settings. But I have to say the spelling of the French language (or Frenglish ?) in that code is very unusual and should be reviewed before this code is even presented.
– Kate
Commented Oct 24, 2021 at 15:51
• @Anonymous I am sure you are correct. I merely put the French into google translate and tried to make sense of it. Same with the name of the functions.. It was an arduous task, as I do not speak a word French. Commented Oct 24, 2021 at 16:39
• @N3buchadnezzar I always write code in English but my teacher told me not to I guess he is not that good anyway I appreciate everything it's really helpful! Commented Oct 29, 2021 at 16:03

# General Changes

• Move any input code to the bottom of the file, inside a main guard.
• Avoid using global variables, instead pass them as parameters.
• Variable names should be in lowercase or snake_case.

# Type Hints

Use type hints to display what types are accepted by a function, and which are returned. It makes understanding your code a bit easier, which in turn saves time reading your program.

# remplir_t

Instead of having a while True, evaluate the bounding condition. This way, if the user inputs a valid number, the loop won't even run. Also, I made this function return a list of inputs by the user. This allows you to avoid messing with global variables, and all around makes the code look a bit nicer.

# voyel_ascii

This code can be reduced to 3 lines. First of all, making the vowel list a string allows you to do the same indexing operation. Next, I'll explain this bit of code:

b = min(ord(ch[0]) + sum(ch[i] in voyells for i in range(len(ch))), 97)


The inside sum bit adds up each true result from the comparison ch[I] in voyells. Since True is 1 and False is 0, the sum function works perfectly for this. Next, the min acts like a boundary, preventing the resulting value from exceeding 97. If the result is greater than 97, the min function will return 97 instead of the result. It's a neat trick that makes your code a lot nicer.

# remplir_tid

I've used a list comprehension to shorten this function to one line. It does the exact same thing you've written, but in a nicer fashion.

Below is the new code I've written. Take a look, and if you have any questions put them in a comment and I'll answer them!

from typing import List

def remplir_t(n: int) -> List[str]:
inputs = []
while n < 1 or n > 9:
n = int(input("N doix etre un valeur de 1 a 9, rentrezy N:  "))
for i in range(n):
indentifacteur = input(f"Entrezy le indentifacteur de le utilisateur {i + 1}: ")
inputs.append(indentifacteur)
return inputs

def voyel_ascii(ch: str) -> str:
voyells = "AEIOUYaeiousy"
b = min(ord(ch[0]) + sum(ch[i] in voyells for i in range(len(ch))), 97)
return chr(b)

def remplir_tid(n: int, t: List[str]) -> List[str]:
return [t[i][0:2] + str(i) + voyel_ascii(t[i]) for i in range(n)]

if __name__ == '__main__':
num_inputs = int(input("Entrezy les numbers de les utilisateurs (1-9): "))

inputs = remplir_t(num_inputs)
result = remplir_tid(num_inputs, inputs)

print(result)
print(inputs)

• A small bit here min does not work (or at least not how you implemented it). If b>90 then we set b=97. With your code b=95 will pass through for instance. Otherwise great answer! Commented Oct 23, 2021 at 20:21
• Truly appreciated! Commented Oct 29, 2021 at 16:04