新手写了个计算化学分子质量
本帖最后由 zzxzrm 于 2020-12-26 15:27 编辑def jisuan_init(che_list,string):
a=0
b=0
c=""
result=0
result_s="Mr("+string+")="
result_s2=""
while True:
if not(a+1>len(string)):
pass
if not(a+1>=len(string)):
pass
else:
if a+1==len(string):
index=che_list.index(string)
result=result+int(che_list)*1
result_s2+="+"+che_list
result_s+="+"+che_list+"="+str(result)
else:
result_s2+="+"+che_list
result_s+="="+str(result)
break
if stringin che_list and (string in che_list or string.isdecimal()):
if not(a+1>=len(string)):#判断有没有到最后一个字符
if string.isdecimal():
index=che_list.index(string)
result=result+int(che_list)*int(string)
result_s=result_s+c+che_list+"×"+string
result_s2=result_s2+c+che_list+"×"+string
a=a+1
else:
index=che_list.index(string)
result=result+int(che_list)
result_s=result_s+c+che_list
result_s2+=c+che_list
print("over","a的值",str(a),string)
else:
if string.isdecimal():
index=che_list.index(string)
result=result+int(che_list)*int(string)
result_s=result_s+c+che_list+"×"+string+"="+str(result)
result_s2=result_s2+c+che_list+"×"+string+"="+str(result)
break
else:
index=che_list.index(string)#57X
result=result+int(che_list)*1
result_s=result_s+c+che_list
result_s2=result_s2+c+che_list
elif string in che_list and not(string in che_list) andnot(string.isdecimal()):
if string+string in che_list:
index=che_list.index(string+string)
a+=1
if not(a+1>=len(string)):
if string.isdecimal():
result=result+(int(che_list)*int(string))
result_s=result_s+c+che_list+"×"+string
result_s2=result_s2+c+che_list+"×"+string
a+=1
else:
result=result+int(che_list)*1
result_s=result_s+c+che_list
result_s2=result_s2+c+che_list
else:
result=result+int(che_list)*1
result_s=result_s+c+che_list
result_s2=result_s2+c+che_list
if not(a+1>=len(string)):
a+=1
#print(a,"节点 下轮循环")
c="+"
else:
result_s+="="+str(result)
break
return "\n\n"+result_s,result,result_s2
class chem_count():
def __init__(self):
chemistry="1 氢 H 1 ;2 氦 He 4 ;3 锂 Li 7 ;4 铍 Be 9 ;5 硼 B 10.8 ;6 碳 C 12 ;7 氮 N 14 ;8 氧 O 16 ;9 氟 F 19 ;10 氖 Ne 20 ;11 钠 Na 23 ;12 镁 Mg 24 ;13 铝 Al 27 ;14 硅 Si 28 ;15 磷 P 31 ;16 硫 S 32 ;17 氯 Cl 35.5 ;18 氩 Ar 40 ;19 钾 K 39 ;20 钙 Ca 40 ;26 铁 Fe 56 ;29 铜 Cu 64 ;30 锌 Zn 65 ;35 溴 Br 80 ;36 氪 Kr 84 ;47 银 Ag 108 ;56 钡 Ba 137 ;78 铂 Pt 195 ;79 金 Au 197 ;80 汞 Hg 200;82 铅 Pb 207;"
chemistry_list=chemistry.split(";")
chemistry_list=chemistry_list
self.che_list=[]
for i in chemistry_list:
self.che_list.append(i.split())
self.che_list.append(i.split())
def jisuan(self,string):
f=string.find("(")
s=string.find(")")
t=s
result_s="Mr("+string+")="
if !=[-1,-1,-1]:
print()
if t >len(string):
t=1
else:
print(t)
if string.isdecimal():
t=string
a=1
else:
t=1
a=0
res=))+"×"+str(t),int(jisuan_init(self.che_list,string)*2)]
print(res,"res")
string=string+string
print(string,"str")
r=jisuan_init(self.che_list,string)
print(r)
return ("\n\n"+result_s+str(r)+"+"+str(res)+"="+str(res+r))
else:
return jisuan_init(self.che_list,string)
if __name__=="__main__":
chemistry="1 氢 H 1 ;2 氦 He 4 ;3 锂 Li 7 ;4 铍 Be 9 ;5 硼 B 10.8 ;6 碳 C 12 ;7 氮 N 14 ;8 氧 O 16 ;9 氟 F 19 ;10 氖 Ne 20 ;11 钠 Na 23 ;12 镁 Mg 24 ;13 铝 Al 27 ;14 硅 Si 28 ;15 磷 P 31 ;16 硫 S 32 ;17 氯 Cl 35.5 ;18 氩 Ar 40 ;19 钾 K 39 ;20 钙 Ca 40 ;26 铁 Fe 56 ;29 铜 Cu 64 ;30 锌 Zn 65 ;35 溴 Br 80 ;36 氪 Kr 84 ;47 银 Ag 108 ;56 钡 Ba 137 ;78 铂 Pt 195 ;79 金 Au 197 ;80 汞 Hg 200;82 铅 Pb 207;"
try:
c=chem_count()
print("友情提醒~~~请注意化学符号的大小写哦~~~")
while True:
push=input()
string=str(push)
if push=="Quit":
print("累死了~~~休息一下吧!!!")
break
elif push!="":
print(c.jisuan(push))
elif push=="":
print("元素符号不会输吗")
except:
print("出错了!!!")
可以 TQL {:10_275:}
页:
[1]