How can I optimize my code and make it more understandable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
26th May 2023, 9:37 PM
Кальян Село
Кальян Село - avatar
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.
27th May 2023, 4:51 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
27th May 2023, 11:05 AM
Lothar
Lothar - avatar
+ 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"))
27th May 2023, 1:55 AM
Bob_Li
Bob_Li - avatar
+ 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.
26th May 2023, 11:29 PM
Orin Cook
Orin Cook - avatar
+ 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.
27th May 2023, 7:12 AM
Bob_Li
Bob_Li - avatar
+ 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.
27th May 2023, 6:52 AM
Bob_Li
Bob_Li - avatar
+ 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
27th May 2023, 7:06 AM
Кальян Село
Кальян Село - avatar
+ 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
27th May 2023, 2:42 AM
Кальян Село
Кальян Село - avatar
+ 1
Tibor I'm too stupid to do that 😅
27th May 2023, 6:48 AM
Кальян Село
Кальян Село - avatar
+ 1
ChatGPT Hehe
28th May 2023, 1:28 PM
Cool-Coder
Cool-Coder - avatar
0
"extremely high stats" . . . Unless those are percent of normal 🥲
28th May 2023, 11:59 AM
Orin Cook
Orin Cook - avatar
0
.... use csv, or json.... but the best choice is still sql (you have sqlite in python)
28th May 2023, 6:37 PM
Mihaly Nyilas
Mihaly Nyilas - avatar