Can someone help me solve why this code wont work

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 2270 Views - Last Post: 25 March 2018 - 02:02 AM Rate Topic: -----

#1 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Can someone help me solve why this code wont work

Posted 24 March 2018 - 02:13 PM

Ok so for school I need to write a program that solves a quadratic equation written in general form.
I need to solve for x with
-b + (square root symbol) b^2 - 4ac / 2
and
-b - (square root symbol) b^2 - 4ac / 2

Here is my code
import math 

numA = int(input("What is the value of A "))
numB = int(input("What is the value of B "))
numC = int(input("What is the value of C "))

x = - numB + math.sqrt(numB (math.pow - 4(numA + numC)))

print (x)



The error reads
x = - numB + math.sqrt(numB (math.pow - 4(numA + numC)))
TypeError: 'int' object is not callable


I have been doing things like trying to break apart the equation but I just can't figure it out. I have also tried using float/str values.

Thank you for your time in reading my issue

Is This A Good Question/Topic? 0
  • +

Replies To: Can someone help me solve why this code wont work

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 02:30 PM

Hi, Welcome to DIC.

You need to check how basic mathematical operations (addition, subtraction, multiplication, division) is written in python.
The attempt indicates that you have jumped into this without the most basic knowledge.
I suggest you find a python introduction and read through the first few chapters.
Was This Post Helpful? 0
  • +
  • -

#3 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 02:35 PM

I have only been coding in python for a few months, will go look at some stuff on the math module. Thank you

This post has been edited by DK3250: 24 March 2018 - 02:51 PM
Reason for edit:: Quote to previous post removed.

Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 02:49 PM

Hm, you probably never coded in python.
Let me help you this much:

Five basic operators.

addition +
subtraction -
multiplication *
division /
exponent **

Example:
>>> a = 3
>>> b = 2

>>> a + b = 5
>>> a - b = 1
>>> a * b = 6
>>> a / b = 1.5
>>> a ** b = 9

Was This Post Helpful? 2
  • +
  • -

#5 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 02:57 PM

What do i type for adding a power to something? like 5^2

This post has been edited by DK3250: 24 March 2018 - 03:07 PM
Reason for edit:: Quote removed - see comment in next post.

Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:06 PM

Adding a power is the same as raising to an exponent - the last example in my post.

When replying, you should use the 'fast reply' found a few lines below the subject. Do not use 'Quote + Reply' unless you really want to quote.

This post has been edited by DK3250: 24 March 2018 - 03:08 PM

Was This Post Helpful? 2
  • +
  • -

#7 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:22 PM

sorry for quote thing

So with what you told me I now have the first part of the equation working but the second part still doesn't work. I am suspecting I am squaring wrong so how would I want to do that correctly?

nvm I just got it working thank you <3
Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:25 PM

You can square in several ways:
# How to square in python:

a = 3

a ** 2
a * a
math.pow(a, 2)

To take the square root:
a ** 0.5
math.pow(a, 0.5)
math.sqrt(a)

Was This Post Helpful? 2
  • +
  • -

#9 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:29 PM

Ok so I have the equation working but I need to round the answer down. I think it is math.floor but I can't seem to get it to work
Was This Post Helpful? 0
  • +
  • -

#10 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:32 PM

Please show you code - much easier to help, then.
math.floor will give the integer lower than your argument.
Is that what you want..?

This post has been edited by DK3250: 24 March 2018 - 03:35 PM

Was This Post Helpful? 2
  • +
  • -

#11 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:50 PM

Ya thats what i want and this is my code
power = 2
forth = 4
import math 

numA = (input("What is the value of A "))
numB = (input("What is the value of B "))
numC = (input("What is the value of C "))



x1 = - numB + (math.sqrt(numB ** power) / 2)

x2 = - numB - (math.sqrt(numB ** power - forth(numA + numB)) / 2)


print (x1)
print(x2)




I had forgotten to add the -4ac after I powered and have been trying to do that but its not working
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is online

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 03:58 PM

Quote

I had forgotten to add the -4ac after I powered and have been trying to do that but its not working

Again, post your attempt. Statements "I can't seem to get it to work" and "its not working" are not helping someone to help you, and posting attempts is required.
Was This Post Helpful? 1
  • +
  • -

#13 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 411
  • View blog
  • Posts: 1,319
  • Joined: 27-December 13

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 04:00 PM

Line 11 is mathematically incomplete - does not reflect the formula for quadratic equations.
Line 13 is also not mathematically correct; but worse, you still forget to use the operator for multiplication.

You should be able to transform the equation to python code..., give one more try.

You do not need to use variables for mathematical constants (2 and 4), - just write the numbers directly in the code.
Was This Post Helpful? 1
  • +
  • -

#14 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • Posts: 2,236
  • Joined: 23-December 08

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 04:00 PM

There's actually another mathematical operator that may be useful here.

Floor Division //
Was This Post Helpful? 0
  • +
  • -

#15 GamersLoveGaming   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-March 18

Re: Can someone help me solve why this code wont work

Posted 24 March 2018 - 04:08 PM

Here is what I have changed
x1 = - numB + (math.sqrt(numB ** 2) - 4 * (numA + numB) / 2)

x2 = - numB - (math.sqrt(numB ** 2) - 4 * (numA + numB) /2 )




The numbers I am putting in are A=3 B=9 and C=-12 the answer is a post to be -4.0 and 1.0 but it is -24 and 6
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3