Can someone help me solve why this code wont work

• (3 Pages)
• 1
• 2
• 3

34 Replies - 1759 Views - Last Post: 25 March 2018 - 02:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=410112&amp;s=1d3c3f0eb645aaa680ab21bbf354208e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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)

```

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.

Is This A Good Question/Topic? 0

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

#2 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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.

#3 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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.

#4 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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.

Five basic operators.

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
```

#5 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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.

#6 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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

#7 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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

#8 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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)
```

#9 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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

#10 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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

#11 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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

#12 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,467
• 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.

#13 DK3250

• Pythonian

Reputation: 395
• Posts: 1,263
• 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.

#14 atraub

• Pythoneer

Reputation: 828
• 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 //

#15 GamersLoveGaming

Reputation: 1
• Posts: 14
• 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