MacOS下VSCode运行Python二次方程代码报错求助
问题描述
在MacOS的VSCode里,除了print("Hello, World"),运行其他Python代码都会弹出以下错误:
Traceback (most recent call last): File "/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/local/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module> cli.main() File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main run() File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "/Users/juliuseners/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "/Users/juliuseners/pythondir/Chapter 1.py", line 14, in <module> if dis>0: TypeError: '>' not supported between instances of 'function' and 'int'
试过按照Homebrew的提示手动修改PATH,问题未解决。附上我运行的代码:
import math #The quadratic is luckily solved for all coefficients a,b,c except for a=0: this diqualifies the equation as quadratic. #I write the function based on following formula: x=(-b+/-sqrt(b^2-4*a*c))/2a a=input("Enter a: ") b=input("Enter b: ") c=input("Enter c: ") def findmyroots(a,b,c): dis=b*b-4*a*c sqrtdis=math.sqrt(abs(dis)) if dis>0: print("The roots are:") print((-b+sqrtdis)/(2*a)) print((-b-sqrtdis)/(2*a)) elif dis==0: print("The roots are:") print((-b)/(2*a)) else: print("Roots are lateral") if a==0: print("Write a real quadratic please..") else:1 findmyroots(a,b,c)
解决方法
根本不是环境变量的问题,全是代码本身的语法和逻辑错误,具体问题和修复如下:
1. 核心问题:变量作用域+缩进错误
dis是定义在findmyroots函数内部的局部变量,函数外部的if dis>0:根本无法访问这个值。报错提示dis是函数类型,大概率是你之前的代码中定义过同名的dis函数,导致全局的dis被覆盖成了函数而非数值。- 判断根的逻辑完全写在函数外面,没有在函数内部执行,等于逻辑无效。
2. 次要问题:输入未转换类型
input()获取的是字符串类型,直接用于数学运算会报错,必须转换为整数或浮点数。
3. 其他语法bug
else:1是无效语法,else后面需要跟缩进的代码块,不能直接写1。- "Roots are lateral"描述错误,应该是复数根,改成中文描述更清晰。
修复后的完整代码
import math # 二次方程求根公式:x=(-b±√(b²-4ac))/(2a),a不能为0 a = float(input("请输入a的值:")) b = float(input("请输入b的值:")) c = float(input("请输入c的值:")) def findmyroots(a, b, c): dis = b**2 - 4*a*c sqrtdis = math.sqrt(abs(dis)) if dis > 0: print("该方程有两个不同的实根:") print((-b + sqrtdis)/(2*a)) print((-b - sqrtdis)/(2*a)) elif dis == 0: print("该方程有一个重根:") print((-b)/(2*a)) else: print("该方程有两个共轭复数根:") print(f"{-b/(2*a)} + {sqrtdis/(2*a)}i") print(f"{-b/(2*a)} - {sqrtdis/(2*a)}i") if a == 0: print("请输入真正的二次方程(a不能为0)!") else: findmyroots(a, b, c)
直接替换原代码,输入数值测试即可,比如输入a=1、b=-3、c=2,会输出根2和1,运行完全正常。
内容的提问来源于stack exchange,提问作者StudentNumberThreeXPlusOne
相关产品推荐
相关产品推荐

