I have a variable called b
who's value is determined as so: b = float(input("What is your y-intercept?"))
. Later in the file, I want to take the value of b
(assuming it is a float) and convert it to a fraction like this: print(Fraction(b))
. I know that something like print(Fraction(0.45))
will print 8106479329266893/18014398509481984
, but print(Fraction('0.45'))
will print 9/20
. How can I have that same result with a variable? Doing print(Fraction('b'))
gives me this error: ValueError: Invalid literal for Fraction: 'b'
.
Code:
from fractions import Fraction
elif equation == 'slope':
slope = input("Do you need to find slope? Type 'yes' or 'no'.")
if slope == 'yes':
y2 = float(input("What's your y2 value?:"))
y1 = float(input("What is your y1 value?"))
x2 = float(input("What is your x2 value?:"))
x1 = float(input("What is your x1 value?:"))
m = y2-y1/x2-x1
print(str(m))
elif slope == 'no':
m = input("What is the slope?")
b = float(input("What is your y-intercept?"))
m = str(m)
b = str(b)
print("Your equation is: y = {}x + {}".format(m,b))
simplify = input("Do you want to simplify this to standard form? Type in 'yes' or 'no'")
if simplify == 'yes':
m = float(m)
b = float(b)
if m < 0:
pos_m = m*-1
if isinstance(b, float):
print(Fraction(b)) #Works, but not what I want
print(Fraction('b')) #Does not work
Fraction(str(b))
This is how you convert the variable's value (rather than its *name) to a string.