Print Statement: As you saw on the previous page the print statement converted the number i into a string and printed it to the command line. This is more or less the equivalent of "say" in snap. Print statements allow a programmer to print values and variables at a chosen location in the code.


>>> x = 5
>>> print(x)
5
                    

Output: Calling the python count_up(num) function with an input produces the following result. (If you'd like you can define count_up yourself, using the code shown on the previous page.)


>>> count_up(5)
1
2
3
4
        

Notice how count_up did not include the input value 5? Remember back to the discussion of the range(x,y) function.It produces a list that does not include y. Glancing at the python function defined above reveals the same mistake found in the previous find_even_nums(x) exercise. To include the final value, the for loop must be changed to:


for i in range(1, num + 1):
        

While Loop

Below is the now familiar count_up function written using a while loop. Unlike the for loop, while does not have a built-in index variable.


def count_up(num):
    i = 1
    while i < num:
        print(i)
        i += 1
                    

The while loop is analogous to the 'repeat until' block in snap However, in Python the loop ends when the condition becomes False. This is opposite of 'repeat until', which waits for its condition to become True.

Exercise 2

Find Exercise 2 in the virus.py file and write an exponent(num, power) function that takes two arguments (a number and an exponent value) and returns the computed result. Below are a few example inputs.


>>> exponent(10, 0)
1
>>> exponent(5, 3)
125
>>> exponent(2, 10)
1024
        

To test your function, type the following into the command line:


python3 virus.py exponent         
        

This will run a series of tests to validate the functionality of your exponent definition. Check the output to make sure it worked!