CIS 1051 Temple University Python Worksheet

User Generated

Gurgn

Programming

CIS 1051

Temple University

CIS

Description

Unformatted Attachment Preview

Loops and Functions Andrew Rosen For each of the following problems, write a function that solves the problem. Demo each function you write by calling it. Creating a Function in Python You can create your own functions in Python. Let’s look at the following program. def manyHellos ( n ): for i in range ( n ): print ( " Hello " ) hellos = int ( input ( " How many hellos do you want ? " )) manyHellos ( hellos ) This program will print out “Hello” a number of times equal to the user’s input. By the way, you shouldn’t use hellos as a variable in your program; that will tell me you’re just copy/pasting. 1 99 Bottles of Beer Write a function that uses a for loop to print out the the lyrics of the infamous “99 Bottles of Beer on the Wall” drinking song. However, this function should take in an int as a parameter and start the lyrics from there. For example, if the function is called with 10 as the parameter, the output should be: 10 bottles of beer on the wall, 10 bottles of beer Take one down, pass it around, 9 bottles of beer on the wall 9 bottles of beer on the wall, 9 bottles of beer Take one down, pass it around, 8 bottles of beer on the wall ... (output continues in the same pattern) ... 1 bottles of beer on the wall, 1 bottles of beer Take one down, pass it around, 0 bottles of beer on the wall 1 2 Multiplication Table Write a function which, given an integer n as an input, prints out an n × n multiplication table. A 4 × 4 multiplication table might look like 1 2 3 4 2 4 6 8 3 6 9 12 But there are many valid ways to present it. 3 Summation of squares Write a function which, given an integer n, uses a for loop to print out the sum of all numbers squared from 1 to n. For example, if the given integer is 5, the program should print out 55, as 12 + 22 + 32 + 42 + 52 = 55. 2 4 Hourglass Write a function that creates the following figure of an hourglass. You must use for loops. This function takes no inputs. |""""""""""| \::::::::/ \::::::/ \::::/ \::/ || /::\ /::::\ /::::::\ /::::::::\ |""""""""""| 5 Slash Figure Write a function, which given an int n, prints out a slash-based ASCII art of size n. Below is an example of what the output looks like at size 4: !!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// And size 6 !!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!////// \\\\\\\\!!!!!!//////// \\\\\\\\\\!!////////// And size 7: !!!!!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!!!!!////// \\\\\\\\!!!!!!!!!!//////// \\\\\\\\\\!!!!!!////////// \\\\\\\\\\\\!!//////////// 3 6 Grading Each problem is worth 20 points, broken down as follows: 12 points The problem is solved as directed. Partial credit may be given for partial solutions at the grader’s discretion. 3 points The code is properly indented and easy to read. 5 points The problem is in a function. 4
Purchase answer to see full attachment
Explanation & Answer:
2 pages
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

At...


Anonymous
Excellent resource! Really helped me get the gist of things.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags