-1
a=input("Write your function")
b=input("Input the derivative of your function ")

def f(x):
   return a

def fprime(x):
   return b


guess= 1
for n in range(1, 100):
   nextguess = guess - f(guess)/fprime(guess)
   print(nextguess)
   guess = nextguess

The error code:

Traceback (most recent call last):
  File "C:\Users\Anton Morian\kod\Projekt.py", line 13, in <module>
    nextguess = guess - f(guess)/fprime(guess)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

2 Answers2

0

First, you dont need funtions here. Second, input() in python returns str. You neet to convert it to int to do math.

I am not a math guy so dont knos what you trying to achieve here but try this code.

a=input("Write your function")
b=input("Input the derivative of your function ")

guess= 1
for n in range(1, 100):
   nextguess = guess - int(a)/int(b)
   print(nextguess)
   guess = nextguess
Rahul
  • 10,830
  • 4
  • 53
  • 88
0

This is my final result, what do u think? And yes I am aware of using global varibles.

And btw, its newtows method

restart = True
import re
from decimal import Decimal
while restart:
  print("Hej ska du vara! Detta är Newtons metod. Jag kan räkna ut nollställen ALLA nollställen, coolt va?!? Testa gärna mig :)")
  a=(input("Skriv din funktion"))
  b=(input("Ange dervatan till din funktion"))

  def f(x):
     return eval(re.sub("x", str(x), a))

  def fprime(x):
     return eval(re.sub("x", str(x), b))

  gissa = None
  inputText = "Ange din gissning av ett nollställe: "

  def getInput():
      global gissa
      global inputText
      gissa = float(input(inputText))

      if fprime(gissa) == 0:
          inputText = "Gissningen ej definierad, ange en ny: "
          return True
      else:
          return False

  while getInput():
      pass

  for n in range(1, 50):
      nextgissa = gissa - f(gissa)/fprime(gissa)
      gissa = nextgissa

  gissa=Decimal(gissa)
  output=round(gissa,2)
  print("Nollstället är", output)
  restart=input("Tryck på valfri tagent för att starta om eller enter för att avsluta!")
  if restart=="":
      restart = False
      print("Hejdå ska du vara!")