Converting inputted fractions into decimals

72 views Asked by At

I'm a python beginner, and I'm trying to create a simple triangle calculator. Right now, I'm trying to have an inputted fraction be turned into a float so that I can multiply it by math.pi and do what I need with it. Im running into an error message that says

y =float((input("Angle B: ")))
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '1/3'

when I input '1/3'.

I've tried using decimal.Decimal(input("Angle B:")) to get around this but it didn't work.

Right now the function looks like:

def chooseradians():
    choice = str(input("1. Side Side Side \n2. Side Angle Side\n3. Angle Side Angle"))
    if str(choice) == str(1):
        x =float(input("Side A: "))
        y =float(input("Side B: "))
        z =float(input("Side C: "))
        sidesidesiderad(x, y, z)
    elif str(choice) == str(2):
        x =float(input("Side A: "))
        y =float((input("Angle b: ")))
        z =float(input("Side C: "))
        SideAngleSiderad(x, y, z)
    elif str(choice) == str(3):
        x =float((input("Angle a: ")))
        y =float(input("Side B: "))
        z =float((input("Angle c: ")))
        #AngleSideAnglerad(x, y, z)
3

There are 3 answers

1
NoDakker On

There is a package named "fractions" that could be imported that does simple fraction conversion. Following is a refactored example of using that package.

from fractions import Fraction

def chooseradians():
    choice = str(input("1. Side Side Side \n2. Side Angle Side\n3. Angle Side Angle "))
    if str(choice) == str(1):
        x =float(Fraction(input("Side A: ")))
        y =float(Fraction(input("Side B: ")))
        z =float(Fraction(input("Side C: ")))
        print("Side A:", x, "Side B:", y, "Side C:", z)
        #sidesidesiderad(x, y, z)
    elif str(choice) == str(2):
        x =float(Fraction(input("Side A: ")))
        y =float((Fraction(input("Angle b: "))))
        z =float(Fraction(input("Side C: ")))
        print("Side A:", x, "Angle b:", y, "Side C:", z)
        #SideAngleSiderad(x, y, z)
    elif str(choice) == str(3):
        x =float((Fraction(input("Angle a: "))))
        y =float(Fraction(input("Side B: ")))
        z =float((Fraction(input("Angle c: "))))
        print("Angle a:", x, "Side B:", y, "Angle c:", z)
        #AngleSideAnglerad(x, y, z)

chooseradians()

Performing a simple test resulted in the following terminal output.

craig@Vera:~/Python_Programs/Triangle$ python3 Fractions.py 
1. Side Side Side 
2. Side Angle Side
3. Angle Side Angle 1
Side A: 3
Side B: 22/33
Side C: 5
Side A: 3.0 Side B: 0.6666666666666666 Side C: 5.0

Now this probably solves half of the battle if any of the values are comprised of a whole number plus a fraction (e.g. "1 1/3"). When testing this type of compound entry, an input error still occurs.

craig@Vera:~/Python_Programs/Triangle$ python3 Fractions.py 
1. Side Side Side 
2. Side Angle Side
3. Angle Side Angle 1
Side A: 1 1/3
Traceback (most recent call last):
  File "/home/craig/Python_Programs/Triangle/Fractions.py", line 24, in <module>
    chooseradians()
  File "/home/craig/Python_Programs/Triangle/Fractions.py", line 6, in chooseradians
    x =float(Fraction(input("Side A: ")))
  File "/usr/lib/python3.10/fractions.py", line 115, in __new__
    raise ValueError('Invalid literal for Fraction: %r' %
ValueError: Invalid literal for Fraction: '1 1/3'

If such compound whole and fractional numbers are going to be allowed for entry, either a subsequent parsing function would need to be built or the whole number portion would need to be converted into a fractional equivalent (e.g. "4/3" which would be the equivalent value for this example). But either way, this should provide you with some possibilities to progress.

0
jayminnie On

You could try to split it by the character "/" by using .split() method, after you ask for the user's input:

user_input = "1/3"

user_input = user_input.split("/")
answer = float(user_input[0])/float(user_input[1])

print(answer)
0
ti7 On

Take a look at SymPy, which can represent fractions (with its Rational type) and make other symbolic representations of incomplete calculations

>>> from sympy import *
>>> a = parse_expr(input('a: '))
a: 1/3
>>> b = parse_expr(input('b: '))
b: 4/5
>>> a, b
(1/3, 4/5)
>>> a + b
17/15
>>> (a + b).n()
1.13333333333333
>>> type(a)
<class 'sympy.core.numbers.Rational'>