Im really new to Python and I just learnt about Quadratic equations in maths, and so i made it made it my project to make a quadratic solver.
But i have a problem i have a line if tocontinue == n: I try to run it in IDLE and i get a popup saying Syntax Error! and it highlights the : at the end... before it also highlighted the = but I made it == and now it doesn't
here is the entire code

global running, y, n, N, Y
running = True
y=y
n=n
Y=y
N=n


def quadratic():
    global a, b, c, x, y
    a = int(input('Enter an A : '))
    b = int(input('Enter an B : '))
    c = int(input('Enter an C : '))
    x = (-b+(b**2-4*a*c)**.5)/(2*a)
    z = (-b-(b**2-4*a*c)**.5)/(2*a)

def answer():
    print(x, 'is positive.')
    print(z, 'is negative.')
    Fraction('x \t\n')
    Fraction('z \t\n')
    print(fractions.Fraction(x), 'is fraction from + in +/-.')
    print(fractions.Fraction((-b-(b**2-4*a*c)**.5)/(2*a)), ' is fraction from - in +/-.')

def intro2():
    global tocontinue, y, n, N, Y
    tocontinue = int(input('Again? (y/n))')
        if tocontinue == n:
            running = False
        else;
            running = True
                           
                           

 def intro():
    global running, intro3, y, Y
    while running:
        quadratic()
        answer()
        intro2()

Im not sure whats really wrong with it? any help much appreciated and also are there any begineers guides for python, its my first programing language I am learning :)
ive read a Byte of Python but i need MOAR!

thanks in advance

Recommended Answers

All 6 Replies

There are several big mistake yes.
And that use of global is not good at all.
Dont use global at all it`s ugly.

Test yout code in idle.
This is wrong.

Fraction('x \t\n')

Could be.

Fraction = ('x \t\n')

But it does not do what you want.

And this will never work.

print(fractions.Fraction(x), 'is fraction from + in +/-.') 
print(fractions.Fraction((-b-(b**2-4*a*c)**.5)/(2*a)), ' is fraction from - in +/-.')

And this what to say.

def intro2():
    global tocontinue, y, n, N, Y
    tocontinue = int(input('Again? (y/n))')
        if tocontinue == n:
            running = False
        else;
            running = True

Please test your code in idle, missing ")"(if not syntax error)

>>> tocontinue = int(input('Again? (y/n))'))
Again? (y/n))y
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    tocontinue = int(input('Again? (y/n))'))
ValueError: invalid literal for int() with base 10: 'y'

You use int here (input in python 3 return a string)

This is how it should have looked.

>>> tocontinue = input('Again? (y/n ')
Again? (y/n y
>>> tocontinue
'y'
>>>

And this i dont want to comment.

def intro():
    global running, intro3, y, Y
    while running:
        quadratic()
        answer()
        intro2()

So some more reading is smart.
Most stuff out there is for 2.x,so my advice is to use that to.
No problem to have both 2.x and 3.x installed.

For python 3.x
http://diveintopython3.org/
http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3.0

And a lot good info on this site.
http://www.daniweb.com/forums/thread20774.html

Thanks alot but just one last question does python have a sort of

+/- thing? because in quadratics it should be +/-sqrt(of yada yada yada....

thanks anyhow

Test yout code in idle.
This is wrong.

Fraction('x \t\n')

Could be.

Fraction = ('x \t\n')

But it does not do what you want.

And this will never work.

print(fractions.Fraction(x), 'is fraction from + in +/-.') 
print(fractions.Fraction((-b-(b**2-4*a*c)**.5)/(2*a)), ' is fraction from - in +/-.')

What im trying to do with the fraction thing is what is sorta displayed on this http://localhost:7464/fractions.html
oh and one other thing, if i dont have global it cant call the definition of what i want can i?
you need the module docs thing open (search for fraction in it)

def intro2():
	global tocontinue, y, n, N, Y, running
	tocontinue = input('Again? (y/n)')
	if tocontinue == n:
		running = False
	else:
		running = True               

def intro():
	global running, tocontinue
	while running:
		quadratic()
		answer()
		intro2()

I ran this in Idle and it said that on line 9 (the def intro(): line there was a syntax error, I cant see any though? all the brackets have their opposite ones, im pretty sure the indentation is fine?


Also when I try running the entire thing
I get the same error but for the defining of the second function ie.

def quadratic():
    global a, b, c, x, z
    a = int(input('Enter an A : '))
    b = int(input('Enter an B : '))
    c = int(input('Enter an C : '))
    x = (-b+(b**2-4*a*c)**.5)/(2*a)
    z = (-b-(b**2-4*a*c)**.5)/(2*a)

def answer(): #HERE IS THE SYNTAX ERROR!
    print(x, 'is positive.')
    print(z, 'is negative.')
    print(fractions.Fraction(x), 'is fraction from + in +/-.')
    print(fractions.Fraction((-b-(b**2-4*a*c)**.5)/(2*a)), ' is fraction from - in +/-.')

Am i forgetting something to close the first function?

thanks

+/- thing?

Yes off course.
Python is very good to use to solve math problems.

Take a look at this

#First we import math
>>> import math
Then we look at what math has to offer.
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
#Then we can test it out.
>>> math.sqrt(9)
3.0
#Dont now what all this stuff do use doc strin " __doc__" and help
>>> math.radians.__doc__
'radians(x) -> converts angle x from degrees to radians'

>>> help(math.tan)
Help on built-in function tan in module math:

tan(...)
    tan(x)
    
    Return the tangent of x (measured in radians).

>>>

def quadratic(): function is maybe ok depend off you shall do.
But the rest is just wrong.

in maths there is still no plus or minus +/- the plus is on top of the - in rl
operand

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.