0

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)
4
  • I don't think Python has anything built-in that parses fractions. You could ask for the numerator and denominator separately, then divide them. Or you could check if the input contains /, then split it and do the division. Commented Feb 2, 2024 at 20:42
  • @KellyBundy You can't possibley mean evil(input("prompt")).... Commented Feb 2, 2024 at 21:00
  • ast.literal_eval(input())? I thought about it, decided it wasn't really appropriate. Commented Feb 2, 2024 at 21:01
  • @Barmar See NoDakker's answer (or the answers for previous questions about the same thing...) Commented Feb 2, 2024 at 21:29

3 Answers 3

1

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'>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

For such compounds: stackoverflow.com/q/1806278
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.