0
How can I make this code better? Is there a way not to repeat myself 3 times?
2 Respuestas
+ 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. ☺️
+ 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.