Lab 3

Objectives:

  • Practice while loops

Print numbers

Write a function called print_numbers(n) that prints a comma-separated list of the numbers 1 through n on a single line. So for example, print_numbers(5) should produce the output

1, 2, 3, 4, 5

Note that the print command starts a new line every time it is issued. To prevent it starting a new line do

print something,

The comma at the end tells it not to start a new line. To print a number followed by a string you can do the following:

print str(2) + "string"

Factorial

Write a function called factorial(n) that returns n!. Recall that n! = n (n-1) ... 1, and that 0! = 1.

Summing even numbers

Write a function called sum_even(n) that returns the sum of the even numbers that are less or equal to n.