Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Numbers and Variables: Python Does the Math 43

SayMyName.py


SayMyName.py - prints a screen full of the user's name


Ask the user for their name


name = input("What is your name? ")


Print their name 100 times


for x in range(100):


Print their name followed by a space, not a new line


print(name, end = " ")


There’s something new in the print() statement in this pro-
gram’s last line: it contains a keyword argument. In this case, the
keyword is end, and we’re telling the program to end each print()
statement with a space (there’s a space between our quotes: " ")
instead of the regular end-of-line character. Print statements
in Python usually end with the newline character, which is like
pressing enter on your keyboard, but with this keyword argu-
ment we are telling Python we don’t want every printout of our
name to be on a new line.
To see this change a little more clearly, modify the last line of
the program to the following, and then run the program:


print(name, end = " rules! ")


If you run this, you’ll see "Your Name rules!" printed 100 times!
The keyword argument end = " rules! " lets us change how the
print() statement works. The end of every print() statement is now
" rules! " instead of a return or enter newline character.
In programming languages, an argument
isn’t a bad thing; it’s simply how we tell a
function, like print(), to do something. We do
so by putting extra values inside the paren-
theses for that function. Those values inside
the print() statement’s parentheses are the
arguments, and the special keyword argu-
ment means that we’re using the keyword
end to change the way print() ends each line
it prints. When we change the end of the
line from the newline character to a simple
space character, words are added to the end

Free download pdf