将字符串转换为除法的 Python 程序



我正在尝试将此字符串转换为除法。 这就是问题所在,我有三个字符串 s1('1/2'(、s2('2/3( 和 s3('+'(的输入。 我应该得到结果 = 7/6

我试图 def string('1/2','2/3','+'(: 结果 = 浮点数(1/2( + 浮点数(2/3(

想不出太多,任何输入?

你可以使用这个:

from fractions import Fraction #this is required to use the "Fraction" command 
s1 = Fraction(1,2) #nominator first, denominator second
s2 = Fraction(2,3)
S = s1+s2
print(S)

您将在显示器上看到以下内容: 7/6

最新更新