Question
*MUST ANSWER USING GRAPHICS.PY PROGRAMS ONLY* (NOT: tkinter import, etc) FIRST TIME PYTHON USER WITH NO EXTRA PROGRAMS, PLEASE EXPLAIN STEP BY STEP WHAT I
*MUST ANSWER USING GRAPHICS.PY PROGRAMS ONLY* (NOT: tkinter import, etc) FIRST TIME PYTHON USER WITH NO EXTRA PROGRAMS, PLEASE EXPLAIN STEP BY STEP WHAT I NEED TO DO.
PLEASE CREATE CODE THAT CAN BE COPY/PASTED WITHOUT ERROR. WILL RATE IMMEDIATELY THANK YOU!
(15 points) Problem 2 gives you practice with interactive graphics that use mouse clicks rather than the console to get user input. It also gives your practice using coordinate transformation to make placing text in the window easier.
a.(10 points) Write a program line1.py as described in Programming Exercise 8 on page 127 of the textbook. Screenshoot of Exercise 8 from book provided below.
b. (5 points) Modify your program from part (a) so you have a second program named line2.py. This program will have the midpoint labeled, offset slightly from the actual location of the midpoint of the line.
Hint: To create labels that combine text and numerical values, you may find using the type conversion function str() helpful. For more, see page 152 of the textbook. Submit your responses to Problem 2 as two separate modules (line1.py and line2.py). Screenshoot of page 152 from book provided below.
8. Line Segment Information. This program allows the user to draw a line segment and then displays some graphical and textual information about the line segment. Input: Two mouse clicks for the end points of the line segment. Output: Draw the midpoint of the segment in cyan. Draw the line. Print the length and the slope of the line. Formulas: dx = 22 I 1 = dy Y2 Yi slope = dy/dx length = dxc2 + dy2 = 152 Chapter 5. Sequences: Strings, Lists, and Files We can implement the first two lines of our algorithm directly in code using string operations we have already discussed: dateStr = input ("Enter a date (mm/dd/yyyy): ") monthStr, dayStr, yearStr = dateStr.split("/") Here I have gotten the date as a string and split it at the slashes. I then unpacked the list of three strings into the variables monthStr, dayStr, and yearStr using simultaneous assignment. The next step is to convert monthStr into an appropriate number (using int again) and then use this value to look up the correct month name. Here is the code: months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] monthStr = months[int (monthStr)-1] Remember the indexing expression int(monthStr)-1 is used because list in- dexes start at 0. The last step in our program is to piece together the date in the new format: print("The converted date is:", monthStr, dayStr+",", yearStr) Notice how I have used concatenation for the comma immediately after the day. Here's the complete program: # dateconvert.py # Converts a date in form "mm/dd/yyyy" to "month day, year" def main(): # get the date dateStr = input("Enter a date (mm/dd/yyyy): ") # split into components monthStr, dayStr, yearStr = dateStr.split("/") # convert monthStr to the month name months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 8. Line Segment Information. This program allows the user to draw a line segment and then displays some graphical and textual information about the line segment. Input: Two mouse clicks for the end points of the line segment. Output: Draw the midpoint of the segment in cyan. Draw the line. Print the length and the slope of the line. Formulas: dx = 22 I 1 = dy Y2 Yi slope = dy/dx length = dxc2 + dy2 = 152 Chapter 5. Sequences: Strings, Lists, and Files We can implement the first two lines of our algorithm directly in code using string operations we have already discussed: dateStr = input ("Enter a date (mm/dd/yyyy): ") monthStr, dayStr, yearStr = dateStr.split("/") Here I have gotten the date as a string and split it at the slashes. I then unpacked the list of three strings into the variables monthStr, dayStr, and yearStr using simultaneous assignment. The next step is to convert monthStr into an appropriate number (using int again) and then use this value to look up the correct month name. Here is the code: months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] monthStr = months[int (monthStr)-1] Remember the indexing expression int(monthStr)-1 is used because list in- dexes start at 0. The last step in our program is to piece together the date in the new format: print("The converted date is:", monthStr, dayStr+",", yearStr) Notice how I have used concatenation for the comma immediately after the day. Here's the complete program: # dateconvert.py # Converts a date in form "mm/dd/yyyy" to "month day, year" def main(): # get the date dateStr = input("Enter a date (mm/dd/yyyy): ") # split into components monthStr, dayStr, yearStr = dateStr.split("/") # convert monthStr to the month name months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started