In this chapter
- Simon explains how to avoid repeating yourself (and why)
- Erik writes his first Python function
- Erik starts using Python’s interactive tool
- Erik improves his function to make his receipt look professional
"Where did we stop yesterday?" Simon asked Erik on the next day.
"You said that I should not repeat myself. And also you said we are going to write our own function today."
"Right! First, tell me what you know about functions so far."
"We used a couple functions already," Erik started to answer. "We used print() and input(). You said that somebody has written them so we can use them. We can use arguments with functions. We just have to put them between the parentheses and the function will do something with the arguments, like print them."
"Everything is right! You are a great student!" Simon smiled. "The important thing about functions is that they can do the same thing but with different arguments. So if you see that you are doing the same thing several times you should look at whether it can be turned into a function. To decide you should look at your repeating code and ask yourself which parts are the same and which are different. Where is your script from yesterday?"
"Here," Erik opened the menu.py file in the editor.