python - 当 elif 行为真时,Break 功能不起作用
问题描述
start = input('Enter quadratic equation: ')
w = start.split()
while len(w) != 7:
start = input('Enter quadratic equation correctly: ')
w = start.split()
while x < len(w):
if "x" in w[x] and '^2' in w[x]:
a = w[x]
if a == "x^2":
a = 1
else:
a = a.replace('x^2', '')
a = int(a)
if w[x-1] == '-':
a = a * (-1)
else:
a = a
elif 'x' in w[x] and '^' not in w[x]:
b = w[x]
if b == 'x':
b = 1
else:
b = b.replace('x', '')
b = int(b)
if w[x-1] == '-':
b = b * (-1)
else:
b = b
elif w[x].isdigit() and 'x' not in w[x] and w[x] != '0':
c = w[x]
elif w[x] == '-' or '+' or '=':
s = 0
elif not w[x].isdigit() and 'x' not in w[x] and w[x] != '-' or '+' or '=':
print('Mistake')
break #Would not this code here work?
x += 1
我试过只做'else'块,但它仍然不起作用。我要做的是检查输入是否实际上是二次方程。
解决方案
错误的条件是elif not w[x].isdigit() and 'x' not in w[x] and w[x] != '-' or '+' or '=':
,它没有做你想要/需要的
w[x] != '-' or '+' or '=':
部分:
- 就好像
(w[x]!='-') or ('+') or ('='):
- 不喜欢
w[x] != ('-' or '+' or '='):
你想要的是w[x] not in '-+='
:和前一个一样
elif w[x] in '-+=':
s = 0
elif not w[x].isdigit() and 'x' not in w[x] and w[x] not in '-+=':
print('Mistake')
break
推荐阅读
- sql-server - 条件更新后触发器
- quicksort - 结合插入排序与快速排序函数关系
- sql - 具有 3 个以上条件的连续 WHERE 子句
- node.js - 本地安装 NPM 模块有什么原因吗?
- django - 在 Django 应用程序中在 Heroku 上编译 SASS/SCSS 文件
- javascript - 如何在 Angular 7 应用程序中构建目录选择器?
- user-defined-functions - 在 Snowflake 中加入 UDF
- nativescript - 通过 NativeScript 在 GAITracker 上调用方法时出错
- javascript - amcharts 更改 ID 数组的填充颜色
- javascript - 为什么我的 Promise.race 实现不起作用?