-2

I'm trying to create a machine that allows a user to input two numbers and determine whether they are approximately equal to each other.

This has been working for whole numbers but as soon as I test it with decimals I get ValueError: could not convert string to float: '.'

InputString=input("Enter 2 numbers in the form Number1,Number2:    ")
SortedInputString=sorted(InputString)
print(SortedInputString[1])
print(SortedInputString[2])

N1=float(SortedInputString[1])
N2=float(SortedInputString[2])

def approx_equal(N1,N2):

    if N1-N2>=1e-6 :
        return True
    else:
        return False
print("Is", N1, "equal to",N2,"?",approx_equal(N1,N2))
Elijah B
  • 7
  • 3

2 Answers2

0
# We obtain input
InputString=input("Enter 2 numbers separated by a comma: ")

# If there are spaces between the values, we replace the spaces with 'None'
if ' ' in InputString:
    InputString.replace(' ', '')

# We split the input at the comma to obtain the two values
InputString = str(InputString).split(',')

# Print the first value
print(InputString[0])

# Print the second value
print(InputString[1])

# Convert the first value to a floating point value
N1=float(InputString[0])

# Convert the second value to a floating point value
N2=float(InputString[1])

# define a function
def approx_equal(Input1,Input2):

    # Condition if True
    if N1==N2:
        return True
    # Else statement (False)
    else:
        return False
# Print output to the terminal
print("Is", N1, "equal to",N2,"?",approx_equal(N1,N2))
Destroyer-byte
  • 121
  • 1
  • 4
0

Well, I sorted it out myself in the end.

InputString=input("Enter 2 numbers in the form Number1,Number2:    ") #this is where a user would input their chosen numbers

N1,N2=InputString.split(",") #this splits the input string

N1F=float(N1) #converts first number input to float
N2F=float(N2) #converts second number input to float
def approx_equal(N1F,N2F): #the fuction

    if abs(N1F-N2F)<=1e-6 :
       return True
    else:
       return False
 print("Is", N1F, "equal to",N2F,"?",approx_equal(N1F,N2F))
Elijah B
  • 7
  • 3