How can I make this code better? Is there a way not to repeat myself 3 times? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I make this code better? Is there a way not to repeat myself 3 times?

https://code.sololearn.com/cZ6E5jk5xD7H/?ref=app

18th Jul 2021, 2:04 PM
Farzam Khonamri
2 Answers
+ 3
There is a lot you can do here ☺️ 1. "if count == 0:" is the condition for stopping the loop, it is needed only at the beginning of the loop; 2. "count -= 1" - this is the loop counter, it is needed at the very end of the loop; 3. «print(f" Too low! You have {count} lives left")» - only one word "low" is changed in this text, which means that you can write a function for displaying a hint instead; 4. instead of the conditions for comparing "guess" with "random_number", you can use an "switch". Finally, instead of a while loop, you can write a recursive function. ☺️
16th Dec 2021, 11:21 PM
Solo
Solo - avatar
+ 2
you could make a function called "check_count" that takes count and random number as arguments and does exactly what the code that's repeated does.
18th Jul 2021, 2:12 PM
Slick
Slick - avatar