-2

(This is how it should appear when I run it.) a = 3; b = 5; c = 4 There are no solutions

So far I have this set up in python and I do not know what I am doing wrong. (note: my professor wants us to call main at the end of the define functions)

import math
import sys
def main():
    print("a = ",a,";b = ",b,";c = ",c,)
    print_quadratic_solution(3, 5, 2)
    a = print_quadratic_solution(a)
    b = print_quadratic_solution(b)
    c = print_quadratic_solution(c)

def print_quadratic_solution(a, b, c):
    a = int(a)
    b = int(b)
    c = int(c)
    discriminant = b**2 - 4*a*c

    if discriminant < 0:
        print("There are no solutions.")
    elif discriminant == 0:
        x = (-b + math.sqrt(discriminant)) / (2*a)
        print("There is a double root at", x)
    elif discriminant > 0:
        x1 = (-b + math.sqrt(discriminant)) / (2*a)
        x2 = (-b - math.sqrt(discriminant)) / (2*a)
        print("The first root is", x1, " and the second is", x2, ".")

main()

The results come out like this:

The first root is -0.6666666666666666  and the second is -1.0 .
Traceback (most recent call last):
  File "C:\Users\Juan1\Documents\Computer Science 1300\EliAssignment6.py", line 23, in <module>
    main()
  File "C:\Users\Juan1\Documents\Computer Science 1300\EliAssignment6.py", line 5, in main
    print("a = ",a,";b = ",b,";c = ",c,)
NameError: name 'a' is not defined

So can someone help me?

Elizabeth
  • 1
  • 3

2 Answers2

0

The error NameError: name 'a' is not defined meant what it says. The variable a has no value before you assign it. So you cannot put it in a print statement.

0

This is a good homework and I think this exercise should teach you how to decipher error messages. I wouldn't give away where you are going wrong right away. However, I would give you this explanation for the error message.

 The first root is -0.6666666666666666  and the second is -1.0 .
Traceback (most recent call last):
  File "C:\Users\Juan1\Documents\Computer Science 1300\ElizabethZamudioAssignment6.py", line 23, in <module>
    main()
  File "C:\Users\Juan1\Documents\Computer Science 1300\ElizabethZamudioAssignment6.py", line 5, in main
    print("a = ",a,";b = ",b,";c = ",c,)
NameError: name 'a' is not defined

The following

NameError : name 'a' is not defined

Means somewhere in the code variable a is accessed/used before it is assigned a value. So, at the point of execution, your computer doesn't know what a is.

Now, where is this point of execution ? To know this you should dig into the line numbers. This is what is a Traceback

Traceback (most recent call last):
  File "C:\Users\Juan1\Documents\Computer Science 1300\ElizabethZamudioAssignment6.py", line 23, in <module>
    main()
  File "C:\Users\Juan1\Documents\Computer Science 1300\ElizabethZamudioAssignment6.py", line 5, in main
    print("a = ",a,";b = ",b,";c = ",c,)

According to your traceback, your computer tried to run the main() function. This function call was made in line number 23 of File "C:\Users\Juan1\Documents\Computer Science 1300\ElizabethZamudioAssignment6.py"

In this main function of the same file, line 5 has a NameError.

Now, do you see yourself assigning/initiating the variable a within the main() function before line 5. So, your error message is trying to tell you to initiate that value. Your computer doesn't know at line 5 what a is.

Hope this helps.

Vasif
  • 1,353
  • 10
  • 25
  • I see other issues with how your function calls are happening . You probably need to fix that too. I would like to hear from you and what you understand, before I write too much. – Vasif Feb 24 '16 at 23:59
  • I see what I did wrong now. Makes sense! Thank you! – Elizabeth Feb 25 '16 at 00:02