Question: Python question Functions For this assignment you will be writing a series of programs that build on one another, so its important to progress through

Python question

Functions

For this assignment you will be writing a series of programs that build on one another, so its important to progress through this assignment in order. You only need to submit the final version of your code as well as the module that you create for part 5.

Part 1

For Part 1 you will write three line-drawing functions.

# function: horizontal_line # input: a width value (integer) # processing: prints a single horizontal line of the desired size # output: does not return anything
# function: vertical_line # input: a shift value and a height value (both integers) # processing: generates a single vertical line of the desired height. the line is # offset from the left side of the screen using the shift value # output: does not return anything
# function: two_vertical_lines # input: a width value and a height value (both integers) # processing: generates two vertical lines. the first line is along the left side of # the screen. the second line is offset using the "width" value supplied # output: does not return anything

Here is a sample program that you can use to test your functions. The expected output is printed below the sample code:

print("Horizontal line, width = 5:") horizontal_line(5) print() print("Horizontal line, width = 10:") horizontal_line(10) print() print("Horizontal line, width = 15:") horizontal_line(15) print() print("Vertical Line, shift = 0; height = 3:") vertical_line(0, 3) print() print("Vertical Line, shift = 3; height = 3:") vertical_line(3, 3) print() print("Vertical Line, shift = 6; height = 5:") vertical_line(6, 5) print() print("Two Vertical Lines, height = 3; width = 3:") two_vertical_lines(3, 3) print() print("Two Vertical Lines, height = 4; width = 5:") two_vertical_lines(4, 5) print() print("Two Vertical Lines, height = 5; width = 2:") two_vertical_lines(5, 2)

Expected Output:

Horizontal line, width = 5: ***** Horizontal line, width = 10: ********** Horizontal line, width = 15: *************** Vertical Line, shift = 0; height = 3: * * * Vertical Line, shift = 3; height = 3: * * * Vertical Line, shift = 6; height = 5: * * * * * Two Vertical Lines, height = 3; width = 3: * * * * * * Two Vertical Lines, height = 4; width = 5: * * * * * * * * Two Vertical Lines, height = 5; width = 2: ** ** ** ** **

Part 2

Next, write 10 new functions that generate the digits 0-9 using your three line functions. The goal here is to render the digits as they would appear on a digital display:

Each function should accept a width argument to control how wide the number should be. You can assume numbers will always be printed with a height of 5. For example, here is the function for the number 1:

# function: number_1 # input: a width value (integer) # processing: prints the number 1 as it would appear on a digital display # using the supplied width value # output: returns nothing def number_1(width): vertical_line(width-1, 5)

And heres a sample program that calls the function a few times:

print("Number 1, width = 5: ") number_1(5) print() print("Number 1, width = 10: ") number_1(10) print() print("Number 1, width = 2: ") number_1(2)

And heres the expected output:

Number 1, width = 5: * * * * * Number 1, width = 10: * * * * * Number 1, width = 2: * * * * *

Heres a sample program that prints all of the numbers (09).

number_0(5) print() number_1(5) print() number_2(5) print() number_3(5) print() number_4(5) print() number_5(5) print() number_6(5) print() number_7(5) print() number_8(5) print() number_9(5)

And heres the expected output:

***** * * * * * * ***** * * * * * ***** * ***** * ***** ***** * ***** * ***** * * * * ***** * * ***** * ***** * ***** ***** * ***** * * ***** ***** * * * * ***** * * ***** * * ***** ***** * * ***** * *

Part 3

Write two new functions that simulate the addition and subtraction operators. Heres some sample code:

plus(5) print() minus(5)

Which will generate . . .

 * * ***** * * ***** 

You can assume the height of these operators is the same as the height of your numbers (5 units).

Part 4

Write a function called check_answer which will determine if a given addition or subtraction problem was solved correctly. Heres the IPO notation for the function:

# function: check_answer # input: two numbers (number1 & number2, both integers); an answer (an integer) # and an operator (+ or -, expressed as a String) # processing: determines if the supplied expression is correct. for example, if the operator # is "+", number1 = 1, number2 = 2 and answer = 3 then the expression is correct # (1 + 2 = 3). # output: returns True if the expression is correct, False if it is not correct

Heres a sample program that you can use to test your function:

answer1 = check_answer(1, 2, 3, "+") print(answer1) answer2 = check_answer(1, 2, -1, "-") print(answer2) answer3 = check_answer(9, 5, 3, "+") print(answer3) answer4 = check_answer(8, 2, 4, "-") print(answer4)

And heres the expected output:

True True False False

Part 5

Move all of your functions into an external module. This can be done by creating a new .py file in the same folder as your source code file. You can then import the functions from this file by using the following syntax:

import myfunctions

. . . assuming that the file you created is named myfunctions.py. You can then call your functions in your main source code file using dot syntax, like this:

myfunctions.check_answer(1, 1, 2, "+")

Part 6

Finally, put everything together and write a program that lets the user practice a series of random addition and subtraction problems. Begin by asking the user for a number of problems (only accept positive values) and a size for their numbers (only accept numbers between 5 and 10 for width). Then generate a series of random addition and subtraction problemsdisplay the numbers to the user with your digital display functions. Then prompt the user for an answer and check the answer using your check_answer function. Your program should also keep track of how many correct questions the user answered during their game. Heres a sample running of the program:

How many problems would you like to attempt? -5 Invalid number, try again How many problems would you like to attempt? 5 How wide do you want your digits to be? 5-10: 3 Invalid width, try again How wide do you want your digits to be? 5-10: 5 Here we go! What is . . . ***** * ***** * ***** * * ***** * * * * * * * = 4 Correct! What is . . . ***** * ***** * ***** ***** ***** * * * * = -5 Correct! What is . . . * * * * * ***** ***** * ***** * ***** = 0 Sorry, that's not correct. What is . . . ***** * ***** * ***** * * ***** * * * * * * * = 3 Correct! What is . . . ***** * ***** * ***** * * ***** * * ***** * ***** * ***** = 4 Correct! You got 4 out of 5 correct!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!