+ 1
How can I optimize my code and make it more understandable?
12 Answers
+ 8
Using a specialized data structure (class or dictionary) to store the details of each person, rather than a single blob of string, makes a huge difference. Suddenly you would have a database where you can compare names by different attributes, sort them, filter them. Right now, your code is just a long if statement, and the representation of each choice is a long unstructured text block.
+ 8
ВСЯКОЧИНИ ,
a different approach would be to use a relational database system like *sqlite* to hold all the different data.
> by using relationships between different tables, we can model specific connections between data and information. the key point is to
separate code from data.
> for a task like this, we have too much data compared to the real required code. doing modifications can get quite difficult to manage.
+ 6
I would suggest storing the entire thing as a dictionary. More efficient, portable and editable. You can save it in it's own file and import it if needed. It is also easier to search and edit.
Partial example:
NAMES = {
#RUSSIAN NAMES
"николай":
"Сила: 80% \nДружелюбность: 60% \nЭнергичность: 90% \nБыстрота реакции: 70% \nКоммуникабельность: 70% \nПрямолинейность: 60% \nСклонность к риску: 80% \nЛидерские качества: 50% \nОрганизаторские способности: 60%",
"анна":
"Творчество: 70% \nСдержанность: 50% \nМягкость: 80% \nВнимательность: 70% \nДушевность: 90% \nПунктуальность: 60% \nСамокритичность: 80% \nОтзывчивость: 70% \nСпокойствие: 60%",
.
.
.
#ENGLISH NAMES
"john":
"Strength: 80% \nFriendliness: 70% \nEnergy: 90% \nQuick thinking: 80% \nCommunication skills: 70% \nStraightforwardness: 60% \nRisk taking: 80% \nLeadership qualities: 80% \nOrganizational skills: 70%",
.
.
.
}
# how to use:
name = input().lower()
print(NAMES.get(name, "name not found"))
+ 4
Looks like a job for OOP, though it's not gonna help a whole lot since each name gets a different set of attributes. But you can at least separate the logic part from the listing of attributes that way, which will make it more readable.
+ 4
ВСЯКОЧИНИ
Keep your code the way it is right now.
When you learn about dictionaries, try to see if you can use that instead.
When you learn about OOP, use what you learn to create a class that uses your data.
Step by step, you will acquire better and better tools and methods.
+ 3
ВСЯКОЧИНИ
As you learn more, you will find the necessity of writing codes that are easy to maintain and efficient to run.
Your question was about how to optimize it.
These are our suggestions.
Tibor Santa explained very well why you should refactor your code.
if-else might work, but it is not a good way to do it.
+ 3
Bob_Li
I will try to do all this, but only when I have learned the necessary knowledge for this.
+ I still have no idea how to implement it
+ 1
Both ideas are good, but they won't change much in the code. I think to leave it as it is, and if there are any ideas, then implement them
+ 1
Tibor
I'm too stupid to do that 😅
+ 1
ChatGPT
Hehe
0
"extremely high stats" . . . Unless those are percent of normal 🥲
0
.... use csv, or json.... but the best choice is still sql (you have sqlite in python)