首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MacOS VSCode Python安装程序

MacOS VSCode Python安装程序
EN

Stack Overflow用户
提问于 2022-11-01 20:50:39
回答 1查看 52关注 0票数 -2

当我运行除您的打印(“Hello,World")以外的其他任何内容时,终端将打印以下内容:

代码语言:javascript
复制
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'

我解决不了这个问题,以前有没有人碰到过这个问题?

尝试手动改变路径使用家庭酿造的建议,但不知怎的,我不能让这个工作。

编辑:我添加了我试图在下面运行的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-11-01 21:30:55

几个问题

  1. 您的if块需要缩进才能在函数中。
  2. 您的输入需要转换为数字,因为您不能对字符串类型进行计算。

相反:

代码语言:javascript
复制
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

#Problem #2: These have to be numeric otherwise your math will throw an error. I'm wrapping in `int` as an example
a=int(input("Enter a: "))
b=int(input("Enter b: "))
c=int(input("Enter c: "))

def findmyroots(a,b,c):
    dis=b*b-4*a*c
    sqrtdis=math.sqrt(abs(dis))

    #Problem #1: Indentation. This all needs to be inside the function. 
    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:
    findmyroots(a,b,c)\

我不知道您对二次公式的实现是否正确(我已经关注了20+多年了),但是这段代码将毫无错误地运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74281643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档