[Tutor] logic ?

Jerry Lake jerryl@europa.com
Thu, 4 Oct 2001 14:47:23 -0700

thank you all for the quick help.

additionally I'm having issues
converting the raw input to float
what I have is as follows

<from terminal>
[upstart@localhost learn]$ ./function4.py
Enter a coordinate for x1: 1
Enter a coordinate for y1: 3
Enter a coordinate for x2: 6
Enter a coordinate for y2: 5
The distance between the points is
Traceback (most recent call last):
  File "./function4.py", line 37, in ?
    print "The distance between the points is", distance(x1, y1, x2, y2)
  File "./function4.py", line 20, in distance
    dx = x2 - x1
TypeError: unsupported operand type(s) for -
</from terminal>


# import the math module
import math

# get coordinates from user input
x1 = raw_input("Enter a coordinate for x1: ")
y1 = raw_input("Enter a coordinate for y1: ")
x2 = raw_input("Enter a coordinate for x2: ")
y2 = raw_input("Enter a coordinate for y2: ")

# convert raw input to floating point numbers

# create function to determine distance between coordinates
def distance(x1, y1, x2, y2):
   dx = x2 - x1
   dy = y2 - y1
   dsquared = dx**2 + dy**2
   result = math.sqrt(dsquared)
   return result

# create function to determine slope of coordinates
def slope(x1, y1, x2, y2):
   x = x1 - x2
   y = y1 - y2
   if (x == 0 or y == 0):
      slope = 0
      slope = x / y
   return slope

# print distance and slope of coordinates to screen
print "The distance between the points is", distance(x1, y1, x2, y2)
print "The slope of the two points is ", slope(x1, y1, x2, y2)


Jerry Lake     -    jlake@US.NET
Interface Engineering Technician

Jerry Lake     -    jlake@US.NET
Interface Engineering Technician

-----Original Message-----
From: Sean 'Shaleh' Perry [mailto:shalehperry@home.com]
Sent: Thursday, October 04, 2001 2:39 PM
To: Python Tutor mailing list
Subject: Re: [Tutor] logic ?

> 2) Your condition in the if statement is wrong. it should read "x==0 or
> y==0",
> although "y==0", or even just "y" will do for this exercise.

to further clarify this, python sees "if (x or y == 0)" as

"if (x) or (y == 0)".

Tutor maillist  -  Tutor@python.org