-2

When I run anything other than your print("Hello, World") the terminal prints the following:

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'

I fail to solve the problem, has anyone ran into this before?

Tried manually changing PATH using homebrews recommendations but somehow I cannot get this to work.

EDIT: Im adding the code I'm attempting to run below:

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)
  • can you give an example of what are you trying? – ggeop Nov 01 '22 at 20:54
  • Just trying to write a program that solves quadratic equations. – StudentNumberThreeXPlusOne Nov 01 '22 at 20:55
  • What is `dis` variable ? – Lesiak Nov 01 '22 at 20:55
  • The traceback is actually showing what you are running and it's not `print("Hello, World")`. So, show us your code! – Klaus D. Nov 01 '22 at 20:57
  • You need to be more specific.. Try to reproduce your error with a simple example. – ggeop Nov 01 '22 at 21:00
  • Your code has an error in it: `if dis>0:` the error message is pretty clear. What exactly are you asking? This seems to have nothing to do with VSCode – juanpa.arrivillaga Nov 01 '22 at 21:02
  • I wrote a comment, but I think my conclusion was incorrect (about what is causing this issue). Instead, your `if` blocks need to be indented to be inside of your function. – JNevill Nov 01 '22 at 21:28
  • 1. Your code has a nesting problem: if dis > 0 (and subsequent lines) is not a part of a findmyroots function. Thus, dis is looked up in global context. 2. Either VSCode or one of the plugins you installed has auto-import featue, and imports dis function from dis module (equivalent to `from dis import dis`) – Lesiak Nov 01 '22 at 21:31
  • @BeRT2me The code is wrong, but confusing error message is due to auto-import feature in vscode or some plugin. – Lesiak Nov 01 '22 at 21:34
  • Thank you all for helping me, been going over it this morning and it turns out I was doing a bad job of indenting the function and if-else statements. I've done C programming where the compiler ignores any and all attempts to make things readable and I'm very new to Python so thank you all again, this really helped. – StudentNumberThreeXPlusOne Nov 02 '22 at 06:15

1 Answers1

0

A couple of problems

  1. Your if block needs to be indented to be inside the function.
  2. Your inputs need to be converted to numeric since you can't do math on string types.

Instead:

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)\

I don't know if your implementation of the quadratic formula is correct (it's been 20+ years since I had to care about that stuff), but this code will run without error.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Thank you for explaining! Just curious: I thought that Python would recognize the input immediately so that I wouldn't need to declare "int", "dynamic typing"(?) I believe I read somewhere.. – StudentNumberThreeXPlusOne Nov 02 '22 at 06:09
  • Python is both strongly typed and dynamically typed (https://stackoverflow.com/questions/11328920/is-python-strongly-typed). The `input()` function returns a string: https://docs.python.org/3/library/functions.html#input – Alexander L. Hayes Nov 02 '22 at 13:28