Python Forum
Simple Distance Between Points Calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Distance Between Points Calculator
#1
hi im new to coding this is only my seventh or so class for python!
today during my class we were assigned to make a simple distance between the points calculator but something went wrong with my code it seems to be skipping over the if and going straight into the else so i think something may be wrong with the if itself but i'm not certain and don't have the necessary knowledge to know just yet
#This Program will ask the user to enter two points in the form
#(x1,y1) and (x2,y2)

import math

x1 = float(input("Enter X1:"))
y1 = float(input("Enter Y1:"))
x2 = float(input("Enter X2:"))
y2 = float(input("Enter Y2:"))

#calculate the distance between the points

theDistance = math.sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))
a = theDistance
b = input("Your Guess?")
if a == b: 
  print("Your Guess is Correct")
  print(theDistance)
else:
  print("Your Guess is Incorrect")
  print(theDistance)
Reply
#2
#This Program will ask the user to enter two points in the form
#(x1,y1) and (x2,y2)
 
import math
 
x1 = float(input("Enter X1:"))
y1 = float(input("Enter Y1:"))
x2 = float(input("Enter X2:"))
y2 = float(input("Enter Y2:"))
 
#calculate the distance between the points
 
theDistance = math.sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))
a = theDistance
# b was not a float
b = float(input("Your Guess?"))
# never compare floats of equality
# use math.isclose, which uses by default a relative
# tolerance of 1e-9
if math.isclose(a, b):
  print("Your Guess is Correct")
  print(theDistance)
else:
  print("Your Guess is Incorrect")
  print(theDistance)
b was not a float. You've forgotten to convert the str to a float.
Never use the == operator to compare floats. Use instead math.isclose(a, b).

Here an example with floating point arithmetic
a = 0.1
b = 0.2
c = a + b

if c == 0.3:
    print("Does not work...")
else:
    print("The result is", c)


if math.isclose(0.3, c):
    print("But this works.")
Here the explanation why you get this strange results:
print(f"https://{0.1 + 0.2}.com")
This should give you the right URL to the page, which explains the problem.
Spade likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple calculator ryza 3 1,109 Sep-21-2022, 04:42 PM
Last Post: deanhystad
  Printing simple calculator answer LCFC2020 2 2,153 Dec-05-2020, 02:07 PM
Last Post: MK_CodingSpace
  simple calculator shakeelthomas 11 6,043 Jul-23-2018, 10:47 PM
Last Post: shakeelthomas
  Distance between indicies of a list johnissa 2 2,990 Apr-25-2018, 01:04 AM
Last Post: johnissa
  Distance between two points! zepel 14 33,402 Apr-28-2017, 08:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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