ameygodse007/Doubts/blob/master/Question 3

I haven’t understood the code please, explain it.Explain it in such a way that I barely know anything about it.

1 Like

Hi thanks for sharing!

Whenever I run into code that I want to understand, I try to go through a checklist of sorts:

  • Visualize Draw a diagram on paper of the components (can be in pseudocode)
  • Identify Highlight techniques I’m unfamiliar with (loops, conditionals, imported libraries, methods)… Then do a quick search so that I at least understand their purpose (even just a small understanding)
  • Test Try to run some test through the code. If i have a lot of time, I always feel paper helps me digest what’s happening better. If i don’t have time, I will print formatted strings everywhere around a portion of the code to just be able to track how the variables and functions are working together.

Example of useful strings

greeting = "Hello, good morning"
print("the greeting is: " + greeting)
#prints: the greeting is: Hello, good morning

test_num = 5
print("the test number is: " + (str)test_num)
#prints: the test number is: 5
#note you need to put str() in front of non-string variables for them to print
  • Ask Finally, if there is something after all that that I still don’t understand, I can have a question that is super specific to my problem, like:
    Why does the following code give me an error?
test_num = 5
print("the test number is: " + test_num)
# TypeError: can only concatenate str (not "int") to str

Enjoy The main takeaway is that this process is very common to get a better understanding of programming. It may feel uncomfortable at first, but as you get used to it you may find that you enjoy breaking the code apart to see what it does! :smiley: