Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework help
#1
So my issue is this we have started using functions and the module we are supposed to import does not import its from a zip file for examples from the book they all run fine when they are opened by themselves but when trying to import them into another program I get no response. Also I have tried multiple OS's downloading the examples file a few times but the same error occurs no matter what. The files also have read and write permissions.
  • Compute Angles Redux. Name the program computeAnglesRedux.py. Write a new version of the ComputeAngles.py program, on pages 66-67, in which you use functions to (i) draw the triangle with the coordinates input by the user, and (ii) abstract the calculations out of the main program into functions,  thus creating a program that is easier to understand and maintain. 

    Your program shall:

    • Define and use the function distance([b]xa, yaxbyb)[/b]which returns the distance between the points (xa, ya) and (xb, yb). (This calculation is already in ComputeAngles.py --see lines 5-7)
    • Define and use the function determineAngle([b]pqr)[/b], which returns the angle in degrees opposite to side of the triangle. (This calculation is already in ComputeAngles.py --see lines 9-11). 
      This function returns the result, in degrees, of evaluating the formula:

      cos−1(p2−q2−r2)−2qr


This is the module that needs to be imported 
   import turtle

# Draw a line from (x1, y1) to (x2, y2)
def drawLine(x1, y1, x2, y2):
    turtle.penup()
    turtle.goto(x1, y1)
    turtle.pendown()
    turtle.goto(x2, y2)

# Write a text at the specified location (x, y)
def writeText(s, x, y): 
    turtle.penup() # Pull the pen up
    turtle.goto(x, y)
    turtle.pendown() # Pull the pen down
    turtle.write(s) # Write a string

# Draw a point at the specified location (x, y)
def drawPoint(x, y): 
    turtle.penup() # Pull the pen up
    turtle.goto(x, y)
    turtle.pendown() # Pull the pen down
    turtle.begin_fill() # Begin to fill color in a shape
    turtle.circle(3) 
    turtle.end_fill() # Fill the shape

# Draw a circle at centered at (x, y) with the specified radius
def drawCircle(x, y, radius): 
    turtle.penup() # Pull the pen up
    turtle.goto(x, y - radius)
    turtle.pendown() # Pull the pen down
    turtle.circle(radius) 
    
# Draw a rectangle at (x, y) with the specified width and height
def drawRectangle(x, y, width, height): 
    turtle.penup() # Pull the pen up
    turtle.goto(x + width / 2, y + height / 2)
    turtle.pendown() # Pull the pen down
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(width)

   
And here is the problem we are supposed to recreate using functions
   import math

x1, y1, x2, y2, x3, y3 = eval(input("Enter six coordinates of three points \
separated by commas like x1, y1, x2, y2, x3, y3: "))
 
a = math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3))
b = math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3))
c = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))

A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c)))
B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c)))
C = math.degrees(math.acos((c * c - b * b - a * a) / (-2 * a * b)))

print("The three angles are ", round(A * 100) / 100.0,  
    round(B * 100) / 100.0, round(C * 100) / 100.0)

   
Here is the error I get every single time
Syntax Error: import UsefulTurtleFunctions from drawLine:
Reply
#2
Must likely be:
from UsefulTurtleFunctions import drawLine 
But if you import all of them it is going to be painful :)

An often-used solution:

# import module a define short alias for it
import UsefulTurtleFunctions  as utf

# then use the module alias to prefix the function names
utf.drawLine(....)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Please read http://python-forum.io/Thread-Modules-part-1 on how to correctly import modules.
Reply
#4
I get a syntax error.
I copy and pasted what you had 

Error:
Syntax Error: utf.drawLine(....): <string>, line 5, pos 18
Reply
#5
Quote:Syntax Error: utf.drawLine(....): <string>, line 5, pos 18
The ... is invalid syntax. He was just illustrating how to use it after importing it.
Recommended Tutorials:
Reply
#6
got it to run import sys, turtle

sys.path.append("/Users/nickzieno/Desktop/Compute\ Angles\ Redux/UsefulT")

Will I be able to use this correctly?
Reply
#7
What are you trying to accomplish by modifying your system path?
Reply
#8
haha I guess not then I am still trying to import a module from my original question but it just does not want to work
Reply
#9
"import filename" should be the only thing you need to do.
Reply
#10
If your module-name.py is located in the script woring directory you can import it directrly.

import module-name as mn
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020