简体   繁体   English

Sympy 公式重新排列器不适用于字母“E”,我不知道为什么

[英]Sympy formula rearranger isn't working with the letter "E" and I can't figure out why

So as the title suggests I'm making a formula rearranger for some physics formulas, and I'm trying to make a function for E=h*f the formula for energy with Plank's constant and frequency.因此,正如标题所示,我正在为一些物理公式制作一个公式重新排列器,并且我正在尝试为 E=h*f 制作一个 function,这是具有普朗克常数和频率的能量公式。

For some reason when you type the letter "E" into the input() of the E_query() function the answer comes out as just "[]".出于某种原因,当您在 E_query() function 的 input() 中键入字母“E”时,答案只是“[]”。 Every other letter, capitol, lowercase, capitol and lowercase, doesn't have this problem.其他所有字母、大写字母、小写字母、大写字母和小写字母都没有这个问题。 It is just the letter E, and I can't figure out why.它只是字母 E,我不知道为什么。

Different equation example:不同的方程示例:

def Fg_query():
Fg_variables = ["G", "M", "m", "r", "Fg"]
print("Fg=(G*M*m)/r^2 is the formula you selected.")
print("What variable are you trying to solve for?")
Fg_userchoice = input()
G = 0.0000000000667259 
if Fg_userchoice in Fg_variables:
   G,M,m,r,Fg = symbols('G M m r Fg')
   r = (G*M*m)/r**2
   l = Fg
   r = solve(l-r, Fg_userchoice)
   l = Fg_userchoice
   print(r)

elif Fg_userchoice is not Fg_variables:
    print("Invalid input")
    Fg_query()

This is the E=f*h function:这是 E=f*h function:

def E_query():
E_variables = ["E","h","f"]

print("E=h*f is the formula you selected.")
print("What variable are you trying to solve for?")
E_userchoice = input()
h = 6.62607004e-34

if E_userchoice in E_variables:
   h,E,f = symbols('h E f')
   r = (h*f)
   l = E
   r = solve(l-r, E_userchoice)
   l = E_userchoice
   print(r)

elif E_userchoice is not E_variables:
    print("Invalid input")
    E_query()

I don't know if it is a problem with the Sympy library, or something I'm doing wrong, but if someone could tell me what's going on that would be a big help.我不知道这是否是 Sympy 库的问题,或者我做错了什么,但如果有人能告诉我发生了什么,那将是一个很大的帮助。

SymPy sometimes lets you get by with entering a string and will sympify it for you. SymPy 有时可以让您通过输入一个字符串来解决问题,并会为您sympify它。 In the case of E , however, the number constant E = exp(1) is created.然而,在E的情况下,创建了数字常数E = exp(1) So to be safe, don't send the user's choice as a string to solve , send it as Symbol(user_choice) .所以为了安全起见,不要将用户的选择作为要solve字符串发送,将其作为Symbol(user_choice)发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM