-3

I've been set a task to iteratively calculate the value of x of the given formula : x3 - 3x + 4 = 0** using trial and error. The code will first plug in -1.5 as its first guess and then work up or down from there to try and get the answer 0. Every time a number is plugged in the equation the code should output "Answer ## is too big/small"

I've started the code but I'm a bit stuck as it only does it twice and then stops. I used a while loop but I don't think I've used it correctly or that it may be the wrong way to go about this problem. Any suggestions/snippets of code will be greatly appreciated.

import cmath
end = ''

num = 0
guess = -1.5
calculation = (guess**3 - guess * 3 + 4)
print(calculation)
while calculation < 0:
    print("Guess is too small")
else:
    print("Guess is too large")

while calculation != 0 and calculation < 0:
    guess = guess + 0.1 
    calculation = (guess**3 - guess * 3 + 4)
else:
    guess = guess-0.1
    calculation = ((guess**3)-3*guess+4)
    print(calculation)
Blinxen
  • 807
  • 2
  • 13
  • 26
Sarah
  • 59
  • 1
  • 1
  • 7
  • Not really related to the question, but as you are comparing floats, this may be helpful: https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python – Valentino Feb 25 '19 at 17:28

3 Answers3

0

You've confused the concepts of while and if. You need both for ... well, for what I think you're trying to do, try the following outline.

while calculation != 0:
    if calculation < 0:
        # Guess is too small; print message and adjust guess
    else:
        # Guess is too large; print message and adjust guess

I think you can fill in the logic for each branch of the "if". Especially note that this will terminate only if you get an exact answer. If you want to work in a little tolerance, you'll have to adjust the exit condition calculation != 0 to have a little "wiggle room".

Prune
  • 76,765
  • 14
  • 60
  • 81
0

Unclear exactly what you're looking for, but this should get you started:

import cmath

guess = -1.5

#put calculations that you use more than once in a function
def calc(a): 
    return round((a**3-a*3+4),1) #round your result

calculation=calc(guess)

while calculation !=0:
    if calculation<0:
        print('Number is too small')
        guess+=0.1
        calculation=calc(guess)
    elif calculation>0:
        print('Number is too large')
        guess-=0.1
        calculation=calc(guess)
Caerus
  • 674
  • 1
  • 8
  • 19
0

I think you need to put everything inside a single while loop, where you check if your calculation matches the expected result, adjust the guess if not, break the loop if yes.

guess = -1.5

while True:
    calculation = ((guess**3)-3*guess+4)
    print(calculation)

    if abs(calculation - 0) < 0.1:
        print("Correct guess: ", guess)
        break

    elif calculation < 0:
        print("Guess is too small")
        guess = guess + 0.1 

    elif calculation > 0:
        print("Guess is too large")
        guess = guess - 0.1

Note that I do not compare calculation == 0 because as you choose a step of 0.1 for guess, it's unlikely that your calculation will be exactly 0. In other words, calculation == 0 will be always false and the loop will never stop. Hence I check that calculation is close enough to 0, using a tolerance value of 0.1 (same of the step used). In python 3.5 or later, you could use math.isclose from the math module to perform this same check.

Valentino
  • 7,291
  • 6
  • 18
  • 34