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)
There is a package named "fractions" that could be imported that does simple fraction conversion. Following is a refactored example of using that package.
Performing a simple test resulted in the following terminal output.
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.
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.