Docsity

I Love Python, Study notes of Computer Programming

Toggle sidebar
bg1
Computer Programming
Python #6 โ€“ I Love Python
Background:
You are to create a function that uses a for-loop to output I love Python.
Assignment:
1. In your Computer Programming folder, create a folder titled first_last_python_6. Start IDLE. Create a new file.
Save it in your first_last_python_6 folder as love_python.py.
2. At the top of the file, create a function called love_python that accepts one parameter called count.
3. Inside the function, you should add the following:
a. A for-loop that prints the string I love Python the number of times as the value of count.
b. Number each line in the output as the output is printed. Allow 2 spaces to output the number.
c. In addition, print the sum of the numbers from 1 to count.
4. As an example, if the value of count were 7, you would print the following:
1 I love Python
2 I love Python
3 I love Python
4 I love Python
5 I love Python
6 I love Python
7 I love Python
The sum of the numbers from 1 to 7 is 28
5. Test your function by putting the following lines of code after your function definition:
love_python(3)
print()
love_python(18)
6. If you did your code correctly, your output should look like this:
1 I love Python
2 I love Python
3 I love Python
The sum of numbers from 1 to 3 is 6
1 I love Python
2 I love Python
3 I love Python
4 I love Python
5 I love Python
6 I love Python
7 I love Python
8 I love Python
9 I love Python
10 I love Python
11 I love Python
12 I love Python
13 I love Python
14 I love Python
15 I love Python
16 I love Python
17 I love Python
18 I love Python
The sum of numbers from 1 to 18 is 171
7. Now replace the 3 lines of code you added in step 5 with these lines of code:
pf2

Partial preview of the text

Download I Love Python and more Study notes Computer Programming in PDF only on Docsity!

Computer Programming

Python #6 โ€“ I Love Python

Background:

You are to create a function that uses a for-loop to output I love Python.

Assignment:

  1. In your Computer Programming folder, create a folder titled first_last_python_6. Start IDLE. Create a new file. Save it in your first_last_python_6 folder as love_python.py.
  2. At the top of the file, create a function called love_python that accepts one parameter called count.
  3. Inside the function, you should add the following:

a. A for-loop that prints the string I love Python the number of times as the value of count. b. Number each line in the output as the output is printed. Allow 2 spaces to output the number. c. In addition, print the sum of the numbers from 1 to count.

  1. As an example, if the value of count were 7, you would print the following:

1 I love Python 2 I love Python 3 I love Python 4 I love Python 5 I love Python 6 I love Python 7 I love Python The sum of the numbers from 1 to 7 is 28

  1. Test your function by putting the following lines of code after your function definition:

love_python(3) print() love_python(18)

  1. If you did your code correctly, your output should look like this:

1 I love Python 2 I love Python 3 I love Python The sum of numbers from 1 to 3 is 6

1 I love Python 2 I love Python 3 I love Python 4 I love Python 5 I love Python 6 I love Python 7 I love Python 8 I love Python 9 I love Python 10 I love Python 11 I love Python 12 I love Python 13 I love Python 14 I love Python 15 I love Python 16 I love Python 17 I love Python 18 I love Python The sum of numbers from 1 to 18 is 171

  1. Now replace the 3 lines of code you added in step 5 with these lines of code:

print() love_python(5) print() love_python(22) print() love_python(45) print() love_python(87)

  1. Run the program.
  2. Create a text document called output.txt in your first_last_python_6 folder. Copy the output from the Python shell, paste it in your output.txt file, and save the file.

Before you turn this assignment in, make sure that your first_last_python_6 folder has 2 files:

  1. output.txt
  2. love_python.py

Zip your first_last_python_6 folder and turn it in in the usual manner.