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?
See https://en.wikipedia.org/wiki/English_in_computing
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."
See https://www.cs.huji.ac.il/~feit/papers/SingleLetter17ICPC.pdf for details
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: "
while not 1 <= (number_of_usernames := int(input(username_msg))) <= 9:
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
MAX_ALLOWED_USERNAMES = 9
USERNAME_RANGE = [i for i in range(1, MAX_ALLOWED_USERNAMES)]
USERNAME_MSG = f"Input the number of usernames ({min(USERNAME_RANGE)}, {max(USERNAME_RANGE)}) inclusive: "
VOWELS = ["a", "e", "i", "o", "u", "y"]
Username = Annotated[str, "An username to encode"]
UsernameEncoded = Annotated[str, "An encoded username"]
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])
def get_usernames() -> list[Username]:
while (number_of_usernames := int(input(USERNAME_MSG))) not in USERNAME_RANGE:
pass
return [
input(f"Write in the username ({i+1}/{number_of_usernames}): {i + 1}: ")
for i in range(number_of_usernames)
]
def transposition(username: Username) -> Identifier:
if (b := ord(username[0]) + vowel_counter(username.lower())) > 90:
b = 97
return chr(b)
def encoding(username: Username, number: int) -> UsernameEncoded:
return username[0:2] + str(number) + transposition(username)
if __name__ == "__main__":
usernames = get_usernames()
encoded_usernames = [
encoding(username, idx) for idx, username in enumerate(usernames)
]
print(encoded_usernames)
print(usernames)
1>N<9
- what are the constraints that your teacher gave you? \$\endgroup\$