Draw a face
Program plan:
- Import the header file.
- Define the “drawFace” method
- Get the “x” and “y” positions
- Get the “p1” and “p2” positions
- Call the “Oval” method for drawing the face
- Set the color to the face
- Draw the face
- Get the “lc” and “rc” position
- Call the “Circle” method
- Set the color to the face
- Call the “draw” method
- Call the “Circle” method
- Set the color to the face
- Call the “draw” method
- Get the “m1”, “m2”, and “m3” values
- Call the “Rectangle” method
- Set the color to the face
- Call the “draw” method
- Call the “Line” method
- Get the center position
- Get the “x” and “y” position
- Call the “Point” method
- Call the “Line” method
- Call the “draw” method
- Get the “x” and “y” position for different variables
- Calculate “t” and “t1” value
- Call the “Rectangle” method
- Call the “draw” method
- Iterate “i” until it reaches 7
- Clone the teeth by calling “clone” method
- Move the face by calling “move” method
- Call the “draw” method
- Define the main method.
- Read the filename from the user
- Get the image width and height from the file
- Set the graph win size
- Set the coords for graph
- Draw the face
- Get the number of faces from the user
- Iterate “i” until it reaches “n” value
- Get the mouse action
- Call the “drawFace” method
- Call the function “main()”.
This Python program is used to get the GIF (Graphic Interchange format) file and draw the smileys when the user clicking the GIF image.
Explanation of Solution
Program:
#import the header file
from graphics import *
#definition of "drawFace" method
def drawFace(center, size, window):
#get the "x" value
x1 = center.getX()
#get the "y" value
y1 = center.getY()
#get the "p1" value
p1 = Point(x1-(.7 * size), y1 - size)
#get the "p2" value
p2 = Point(x1+(.7 * size), y1 + size)
#call the "Oval" method
head = Oval(p1, p2)
#fill the color
head.setFill("white")
#call the "draw" method
head.draw(window)
#get the "lc" value
lc = Point(x1 - .2 * size, y1 + .6 * size)
#get the "rc" value
rc = Point(x1 + .2 * size, y1 + .6 * size)
#call the "Circle" method
leftEye = Circle(lc, .13 * size)
#fill the color
leftEye.setFill("white")
#call the "draw" method
leftEye.draw(window)
#call the "Circle" method
rightEye = Circle(rc, .13 * size)
#fill the color
rightEye.setFill("white")
#call the "draw" method
rightEye.draw(window)
#get the "m1" value
m1 = Point(x1 - .3 * size, y1 - .5 * size)
#get the "m2" value
m2 = Point(x1 + .3 * size, y1 - .25 * size)
#get the "m3" value
m3 = Point(x1 + .3 * size, y1 - .5 * size)
#call the "Rectangle" method
mouth = Rectangle(m1, m2)
#fill the color
mouth.setFill("white")
#call the "draw" method
mouth.draw(window)
#call the "Line" method
leftLip = Line(m1, Point(x1- .3 * size, y1 - .25 * size))
#get the center position
mLeftCent = leftLip.getCenter()
#get the "x" position
mLCx = mLeftCent.getX()
#get the "y" position
mLCy = mLeftCent.getY()
#call the "Point" method
mRightCent = Point(x1 + .3 * size, mLCy)
#call the "Line" method
lip = Line(mLeftCent, mRightCent)
#call the "draw" method
lip.draw(window)
#get the "x" positions
m1x = m1.getX()
m3x = m3.getX()
#get the "y" positions
m1y = m1.getY()
m2y = m2.getY()
#calculate the "t" value
t = m1x - m3x
#calculate the "t1" value
t1 = Point(m1x - (1/8 * t), m2y)
#call the "Rectangle" method
teeth = Rectangle(m1, t1)
#call the "draw" method
teeth.draw(window)
#iterate "i" until it reaches 7
for i in range (7):
#clone the face
t2 = teeth.clone()
#call the "move" method
t2.move(-i * (1/8 * t), 0)
#call the "draw" method
t2.draw(window)
#definition of main method
def main():
#get the input filename from the user
fname = input("Enter filename: ")
infile = Image(Point(10, 10), fname)
#get the width of the image
wWidth = infile.getWidth()
#get the height of the image
wHeight = infile.getHeight()
#set graph win name
window = GraphWin('Smile!', wWidth,wHeight)
#set the coords
window.setCoords(0, 0, 20, 20)
#draw a smiley
infile.draw(window)
#get how many faces the user wants to draw
n = eval(input("How many faces should we block? "))
#iterate "i" until it reaches "n"
for i in range(n):
#get the mouse action
point = window.getMouse()
#draw a face by calling the method "drawFace"
drawFace(point, 3, window)
#call the "main" method
main()
Output:
Enter filename: test_img.gif
How many faces should we block? 2
>>>
Screenshot of “Smile!” window
Want to see more full solutions like this?
Chapter 6 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- Write a program that reads student data from a file, compute their GPA and writes the results to a different file. 1. The user should have the option to either enter their text file that contains the student grades or use the provided text file that contains theinformation. The data in "indata.txt" should look similar to this, Lara_Croft75 70 91 69 89Chris_Redfield68 88 79 85 94Johnny_Cochran69 98 95 77 80Wanda_Maximoff84 86 98 95 92Luke_Skywalker74 96 80 98 97William_Kurt89 52 99 81 58Samuel_Jackson50 96 50 64 95END_OF_FILE This is supposed to be in python. This is what I have so far. I have no experience I apologise. I am trying to finish the GPA calculating program currently but am having trouble finishing it. grade = input("enter grades")points = 0 if grade is 90-99:total_points = 4.0if grade is 80-89:total_points = 3.0if grade is 70-79:total_points = 2.0if grade is 60-69:total_points = 1.0if grade is 50-59:total_points = 0.0gpa = total_points/len(grade)print(gpa,"is gpa")arrow_forwardWrite a program that asks any number of students to enter their final grade and enables you to store these grades into a grades.txt plain text file. Then it reads the grades from the grades.txt file, and displays the individual grades and their count and average.arrow_forwardAssume that infile has been properly defined to read data from a file and that a file of integers has been properly opened. If you don't know how many integers are in the file, which loop(s) would be appropriate to read in all the values from the file? Variable n has been defined as an int. 1. while(! infile.eof()) 2. for(int x = 0; x < 1000000; x++) 3. while(infile >> n) Answers: A. Both 2 and 3 B. 1, 2, and 3 C. Both 1 and 2 D. Both 1 and 3 E. 3 onlyarrow_forward
- Write a program that reads the scores from numbers.txt a line at a time and displays their total and average. Since you don’t know how many scores, you will need to keep looping until readline is blank. num.txt is a file that contains a list of scores. num.txt 10 25 36 45 89 42 54 create a program that reads the scores from num.txt file one line at a time. The program should display their total and average. since the number of scores is unknown, the program must keep looping until readline is blank. Sample Output: There are 7 scores The total is 301 The average is 43.0arrow_forwardWrite a program that reads student information from a text file, then creates a text file that records the course grades of the students and a final grade. Each row of the .txt file contains the Last Name, First Name, Midterm1 score, Midterm2 score, and the Final score of a student, each separated by a space. A sample of the student information is provided in StudentInfo.txt below. Assume the number of students is at least 1 and at most 20. The program performs the following tasks: • Read the file name of the .txt file from the user. • Open the .txt file and read the student information using readline() or readlines(). • Compute the average exam score of each student. • Assign a letter grade to each student based on the average exam score in the following scale: ◦ A: 90 =< x ◦ B: 80 =< x < 90 ◦ C: 70 =< x < 80 ◦ D: 60 =< x < 70 ◦ F: x < 60 • Output the first names, last names, exam scores, and letter…arrow_forwardUsing a text editor, create a file that contains a list of at least 15 six-digit account numbers. Read in each account number and display whether it is valid. An account number is valid only if the last digit is equal to the remainder when the sum of the first five digits is divided by 10. For example, the number 223355 is valid because the sum of the first five digits is 15, the remainder when 15 is divided by 10 is 5, and the last digit is 5. Write only valid account numbers to an output file, each on its own line.*Note that the contents of the file AcctNumsIn.txt will change when the test is run to test the program against different input. AcctNumsIn.txt 345619789400871208901156984334723422172257100000273699237485761200555500936716763542999995 import java.nio.file.*;import java.io.*;import java.nio.channels.FileChannel;import java.nio.ByteBuffer;import static java.nio.file.StandardOpenOption.*;public class ValidateCheckDigits {public static void main(String[] args) {Path…arrow_forward
- Using a text editor, create a file that contains a list of at least 15 six-digit account numbers. Read in each account number and display whether it is valid. An account number is valid only if the last digit is equal to the remainder when the sum of the first five digits is divided by 10. For example, the number 223355 is valid because the sum of the first five digits is 15, the remainder when 15 is divided by 10 is 5, and the last digit is 5. Write only valid account numbers to an output file, each on its own line.*Note that the contents of the file AcctNumsIn.txt will change when the test is run to test the program against different input. AcctNumsIn.txt345619789400871208901156984334723422172257100000273699237485761200555500936716763542999995 ValidateCheckDigits.javaimport java.nio.file.*;import java.io.*;import java.nio.channels.FileChannel;import java.nio.ByteBuffer;import static java.nio.file.StandardOpenOption.*;public class ValidateCheckDigits {public static void…arrow_forwardUsing a text editor, create a file that contains a list of at least 15 six-digit account numbers. Read in each account number and display whether it is valid. An account number is valid only if the last digit is equal to the remainder when the sum of the first five digits is divided by 10. For example, the number 223355 is valid because the sum of the first five digits is 15, the remainder when 15 is divided by 10 is 5, and the last digit is 5. Write only valid account numbers to an output file, each on its own line. Note that the contents of the file AcctNumsIn.txt will change when the test is run to test the program against different input.arrow_forwardWrite a program that reads a text and outputs the letters, together with their counts, as explainedbelow in the function printResults. Your program should not have any global variables. Allinformation should be accessed through the parameters of the functions. Your program should:have the at least the following functions:• Function main – calls all the functions• Function openFile – opens the input and ouput files. This program has the file streamdeclare a refence parameter. An appropriate error message should be displayed if the filedoes not exist. The program must ask the user for the names of the input and output files.• Function countLetters – counts every of uppercase letters A – Z and lower-case lettersfrom a – z in the text file opened in the function openFile. This information should be storedin an array of structures. Both the array and file identifiers must be passed as parameters.• Function printResults – prints the number of uppercase letters for every occurrence of A–Zand…arrow_forward
- Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line associated with that number. An example file and the program input and output is shown below: example.txt Line 1.Line 2.Line 3. Enter the input file name: example.txt The file has 3 lines. Enter a line number [0 to quit]: 2 2 : Line 2. The file has 3 lines. Enter a line number [0 to quit]: 4 ERROR: line number must be less than 3. The file has 3 lines. Enter a line number [0 to quit]: 0 Make sure the program gracefully handles a user entering a line number that is too high.arrow_forwardWrite a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line associated with that number. An example file and the program input and output is shown below: example.txt Line 1.arrow_forwardThis is Python Program 5: Improve the file copy program from Lab 9 so that it will make sure the user enters a file that exists. If a file doesn’t exist, the open function will raise the FileNotFoundError. You do not need to check if the destination exists since a file doesn’t need to exist to write it. Instead of copying the content of the source as exact, it only copies non-empty lines and it also prepend each line with a line number. A line is empty if the length of the line stripped of white space from both end is 0. The code segment below shows how to strip white space and getting the length of the string. If the length of the line is zero, the line is a blank line. line1 = " " line2 = " aa " print(len(line1.strip())) # 0 print(len(line2.strip())) # 2 In the sample run, “add.py” is the source file and “add-copy.py” is the destination file. Note that “add-copy.py” contains only non-blank lines from “add.py” and each line in “add-copy.py” is labelled with a line number. Sample run:…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning