Tengo este programa que hice, pero por alguna razón no importa matemáticas, por favor ayúdenme y háganme saber qué le pasa [cerrado]

Nov 09 2020
from math import sqrt
def f(x) :
    print("f(x) call g(x)= ", g(x))
    print( "f(x) call sqrt(h(x)) = ", sqrt(h(x)))
    print ()
    return g(x) + math.sqrt(h(x))

def g(x) :
    return 4 * h(x)           
    print("g(x) call h(x)= ", h(x))
    
def h(x) :
    return x * x + k(x) - 1 
 #   print("h(x) call k(x)= ", k(x))

def k(x) :
    return 2 * (x + 1) 
  #  print("k(x) call, 2 * (x+1) = ", 2 * (x +1))

x1 = f(2)
print("x1 = ",x1)
print ()

x2 = g(h(2)) 
print("x2 = ",x2)
print ()

x3 = k(g(2) + h(2)) 
print("x3 = ",x3)
print ()

x4 = f(0) + f(1) + f(2)

print("x4 = ",x4)
print ()

x5 = f(-1) + g(-1) + h(-1) + k(-1) 
print("x5 = ",x5)
print ()

El error lo que obtengo:

NameError                                 Traceback (most recent call last)
<ipython-input-28-d083b9e93ab7> in <module>()
     18   #  print("k(x) call, 2 * (x+1) = ", 2 * (x +1))
     19 
---> 20 x1 = f(2)
     21 print("x1 = ",x1)
     22 print ()

<ipython-input-28-d083b9e93ab7> in f(x)
      4     print( "f(x) call sqrt(h(x)) = ", sqrt(h(x)))
      5     print ()
----> 6     return g(x) + math.sqrt(h(x))
      7 
      8 def g(x) :

NameError: name 'math' is not defined

Respuestas

2 sofia Nov 09 2020 at 15:09

¡olvidaste importar matemáticas primero! importó sqrt de matemáticas, pero aún no ha definido las matemáticas, por lo que no sabe qué hacer.

agrega un simple

import math

al principio de su código y debería ejecutarse!

1 dimay Nov 09 2020 at 15:09

Trata de cambiar

return g(x) + math.sqrt(h(x))

a

return g(x) + sqrt(h(x))