我怎样才能将数字保留为 x 的根



所以我的代码看起来像这样,带有注释:

import math

#I want to do this : If the figure is a triangle it's side , noted with l = r(Stands for radius) square root 3 
#another thing(Which I don't know how to spell in english) , noted with a = r/2 , it's area based on the side(noted with Al)
# = (l**2*Square root of 3) / 4 and it's area based on the radius = (3r**2*Square root of 3)/4
#My problem is that I want to keep the square root of 3 as a number without .07214214312412etc. and if it isn't a perfect
#square keep it like root3 , or something like that if it's possible
figure = input("What is the figure?")
figure = figure.lower()
r = int(input("What is the radius?"))
if figure == "triangle" :
    l = str(r) + " Square root of 3"
    a = int(r) / 2
    l = float(l)
    Al = str((l**2/4)) + " Square root of 3"
    Ar = str(3*r**2/4) + " Square root of 3"
float()字符串

" Square root of 3"是荒谬的。我猜你正在尝试使用平方根符号,那么我认为你必须使用 sympy 库,你的代码遵循

from sympy import *
figure = input("What is the figure?")
figure = figure.lower()
r = int(input("What is the radius?"))
x=sqrt(3)
if figure == "triangle" :
    l=r*x
    a = float(r) / 2
    Al = (l**2/4)*x
    Ar = (3*r**2/4)*x

此外,如果您想将其用作字符串,就像您在代码中所做的那样,您可以像这样使用

from math import *
figure = input("What is the figure?")
figure = figure.lower()
r = int(input("What is the radius?"))
x="*sqrt(3)"
if figure == "triangle" :
    l=str(r)+x
    a = float(r) / 2
    Al = str(eval(l)**2/4)+x
    Ar = str(3*r**2/4)+x

我可能对你在这里得到的东西感到困惑 - 你是说你只是想基本上做

root3 = math.sqrt(3)

还是别的什么?

最新更新