Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python Example C

Let’s say we want to create a function that prints an input string three times. Breaking down the steps of that program:

  • Get input string

  • Print the string (3x)

We might also need some way of tracking how many times we’ve printed the string so it stops at three.

One way we could approach this would be using a count variable and a while statement.

# assign count to 0
count = 0

# get input string
message = input("Type your message here: ")

# while statement
while count < 3:
    print(message) # print message
    count += 1 # reassign count

Thinking through how this program runs:

  • count starts at 0 and increases by an increment of 1 each time the code nested under while runs

  • The code nested under while will keep running until the initial condition (count < 3) is no longer true

We can test this program to make sure the output is what we expect. How that we have a program that prints an input string three times, we can create the named function.

# function definition
def printThreeTimes(string):

Then under the def keyword will go the lines of code that execute our program.

# function definition
def printThreeTimes():
    count = 0 # assign count
    message = input("Type your message here: ") # get input message
    while count < 3: # while statement
        print(message) # print message
        count += 1 # reassign count

Now when we run this block of code, there won’t be any output because we are just creating or defining the printThreeTimes function. When we need to use the function, we can call it using its name.

# sample function call
printThreeTimes()

Putting It All Together

# function definition
def printThreeTimes():
    count = 0 # assign count
    message = input("Type your message here: ") # get input message
    while count < 3: # while statement
        print(message) # print message
        count += 1 # reassign count

# function call
printThreeTimes()

But Modifying It Slightly

But let’s say we don’t want to use an input() statement as part of the function- what if we wanted to pass a specific value to the function?

First, let’s build a program that accomplishes this task.

# assign string
message = "There's no place like home"

# assign count
count = 0

# while statement
while count < 3:
    print(message) # print message
    count += 1 # reassign count

To create the function:

def printThreeTimes(message):
    count = 0 # assign count
    while count < 3: # while statement
        print(message) # print message
        count += 1 # reassign count

Remember in this scenario, we are going to pass a specific value to the function, rather than asking for an input as part of the function.

So how would we call our modified printThreeTimes function? We would pass a string object to the function.

# assign string
message = "There's no place like home"

# function definition
def printThreeTimes(message):
    count = 0 # assign count
    while count < 3: # while statement
        print(message) # print message
        count += 1 # reassign count

# function call
printThreeTimes(message)

for Loop Approach

While the previous example used a while statement to build this program, we could also use a for loop.

# core program
for x in range(3):
  print("There's no place like home")
# function definition
def printThreeTimes(message):
  for x in range(3): # for loop
    print("There's no place like home") # output message
printThreeTimes("There's no place like home") # function call