python 中if...else嵌套的问题

weixin_42028907 2018-04-20 03:27:00
初学python,自己写了段代码,但是,运行程序后,当我任意输入一个单词,比如mono,本来执行结果应该是:
You entered a wrong crystal system
但,却执行了嵌套的if else 中的else下面的代码。不知道什么原因。

crystalSystem = input("Please enter the crystal system of your phase:")
if crystalSystem == 'cubic' or 'tetragonal':
if crystalSystem == 'cubic':
a1 = input("Please enter the lattice parameter: a=")
a2 = float(a1)
Y = (a2 - 5.1159)/0.001547
print("the content of YO1.5 is " + str(Y) + "mol%")
else :
a1 = input("Please enter the lattice parameter: a=")
c1 = input("Please enter the lattice parameter: c=")
a2 = float(a1)
c2 = float(c1)
Y= (1.0225 - (c2/(a2*2**0.5)))/0.0016
print("the content of YO1.5 is " + str(Y) + "mol%")
else:
print("You entered a wrong crystal system")]
...全文
1231 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
欢乐的小猪 2018-04-25
  • 打赏
  • 举报
回复
if crystalSystem == 'cubic' or 'tetragonal' 这句话等价于 if crystalSystem == ('cubic' or 'tetragonal' ) 而( 'cubic' or 'tetragonal') 恒为 True
MaMMoN2017 2018-04-24
  • 打赏
  • 举报
回复
引用 2 楼 qq_34618538 的回复:
if crystalSystem == 'cubic' or crystalSystem == 'tetragonal': 判断条件不对

if crystalSystem in ('cubic', 'tetragonal')
更好看些.
mifengwei 2018-04-20
  • 打赏
  • 举报
回复
if crystalSystem == 'cubic' or crystalSystem == 'tetragonal': 判断条件不对
oyljerry 2018-04-20
  • 打赏
  • 举报
回复
你的判断条件有问题
if crystalSystem == 'cubic' or crystalSystem == 'tetragonal':

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧