The main problem with your python code!

people often get errors in their codes, but sometimes no errors at all, so why is it?

How we understand the code:

I have stumbled upon a Reddit post that into their lines of code, had something like this:

[...] # a lot of prints
def function(msg):
    input(msg)

n1 = function("Name:")
[...] # other data is gathered

('Name:{}'.format(n1)) # forgot print.

What is the problem with this code? Well, a lot, first of all, it has forgotten to print, and even if he did not, it will still print out “name:None” since he forgot to return the correct code.

The second prob is that he is using very bad variables, like “n1” instead of name, since it is unique, or names as a list

Why this bullshit is important?!?!?

Well, let’s say you have a code, with around 1.000 lines, how the f*** will you know what is “n1”? well commenting, right? but do you? It is way easier to make a line that doesn’t need a comment in the first place than commenting on every line.

Also, we need to forget about how we write a document when we are writing code, did you see the error in his code about the return of a function? Well if we think in logical terms, it is obvious it should return what was on input, but it returned None, it was because it wasn’t explicitly told to return something, it also happens is some if’s like a “and” + “or” combo.

if True and True or False and True:
    print("True") # will print, but works like (True and True) or (False and True)
# another example:
if 10 > 1 or 10 > 11 and 0 < 1:
   print("yeah...") 
   # logical issue, we can think of (True or False) and (True)
   # but it is (True) or (False and True) 

Well… that’s it

You can call me mister obvious right now, but starters always get stuck into it.

Leave a Comment