Interesting things in ex7 about "end" in python

In ex7, I was asked to print “Cheese Burger” by defining 12 different variables.
The code is like this:

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print(end1 + end2 + end3 + end4 + end5 + end6, end = " ")
print(end7 + end8 + end9 + end10 + end11 + end12)

And the output is “Cheese Burger”.
Then I want to print 10 "Cheese ", which is inspired by this line:

print("." * 10)

So I simply add a *10 behind 【end1 + end2 + end3 + end4 + end5 + end6, end = " "】like this:

print((end1 + end2 + end3 + end4 + end5 + end6, end = " ") * 10)

And the error occured at the “=” point.
After searching and asking friends, I found that “end” is a kind of command that uses some letters to replace the newline function. If you uses end, then in the next line, your output will share the same line with this line.
Therefore, I change my code like this:

print((end1 + end2 + end3 + end4 + end5 + end6 + " ") * 10, end = "")

And that runs perfectly like this:

Cheese Cheese Cheese Cheese Cheese Cheese Cheese Cheese Cheese Cheese Burger

It is fun to research and debug independently, but I think it is also important for biginners to understand what you are really doing. It will be better to add the explanation of “end” command in the LPTHW ex7.

I totally agree with researching, debugging and exploring. The study drills in the book really try to enforce this approach too. One of the mistakes I initially made was just covering the bare minimum in the study drills. I gave me a false sense of understanding that later transpired to be false when the complexity increased.

Hmm, it’s not a command it’s an argument, and explaining what that is right there is pretty difficult without getting into crazy amounts of explanation about function calls. Just keep going and you’ll find out more about why.

1 Like

Thank you for replying! Since I am not so good at English, I cannot understand the difference between “command” and “argument”. But I will keep it in my mind and see if it can be solved in later exercises.

So, command or function are the words before (). Argument or parameter is the words inside the (). Here’s an example:

make_a_sandwich(2, "Peanut Butter")

The function (also known as command in some places) is make_a_sandwich. The arguments, or parameters, are 2 and "Peanut Butter". Doing that you are telling python to make you 2 peanut butter sandwiches.

1 Like