简体   繁体   English

功能输入

[英]Input in Functions

How can I ask for user input in the nthroot function? 如何在nthroot函数中要求用户输入? I want the function to ask input for the power of x and the next term. 我想让函数要求输入x和下一项的幂。 (I want the program to ask for power and next term, I only used 3 and 81 respectively as a check) (我想让程序要求加电和下个学期,我只分别使用了3和81作为检查对象)

def derivative(f, x, h):
   return (f(x+h) - f(x-h)) / (2.0*h)  

def nthroot(x):
   return x**3 - 81   # just a function to show it works


def newraph(f, x0, h):
   Xold = x0
   Xnew = Xold + 10* h  
   while (abs(Xold - Xnew) > h):  
      Xold = Xnew 
      Xnew = Xold - f(Xnew) / derivative(f, Xold, h)  #NewtonRaphson formula
   return Xnew

trueroot = newraph(nthroot, 1, 0.000001)    
print "The root is", trueroot

When I tried using the int(raw_input()) like this : 当我尝试像这样使用int(raw_input())时:

def derivative(f, x, h):
  return (f(x+h) - f(x-h)) / (2.0*h)  

def nthroot(x):
   root = int(raw_input("Please Enter a nth root (n) : "))
   number =  int(raw_input("Please Enter a number (x): "))
   return x**(root) - (number)

def newraph(f, x0, h):
   Xold = x0
   Xnew = Xold + 10* h  
   while (abs(Xold - Xnew) > h):  
      Xold = Xnew 
      Xnew = Xold - f(Xnew) / derivative(f, Xold, h)  #NewtonRaphson formula
   return Xnew

trueroot = newraph(nthroot, 1, 0.000001)    
print "The root is", trueroot

The prompt for the input repeats several times. 输入提示重复多次。 How do you end that? 你怎么结束的?

Try generating your Nthroot function on the fly with the wanted values already in there. 尝试动态生成带有所需值的Nthroot函数。 These values will then be reused in every vall to "f" in newraph. 然后,将在每个变量中将这些值重用到newraph中的“ f”。

This method will allow you to keep using arbitrary functions for newraph (even ones where you don't need user input or completely different inputs). 这种方法将使您能够继续对newraph使用任意函数(甚至在不需要用户输入或完全不同的输入的情况下)。

def derivative(f, x, h):
  return (f(x+h) - f(x-h)) / (2.0*h)  

def generateNthrootFunc():
   root = int(raw_input("Please Enter a nth root (n) : "))
   number =  int(raw_input("Please Enter a number (x): "))
   def nthroot(x):
      return x**(root) - (number)
   return nthroot

def newraph(f, x0, h):
   Xold = x0
   Xnew = Xold + 10* h  
   while (abs(Xold - Xnew) > h):  
      Xold = Xnew 
      Xnew = Xold - f(Xnew) / derivative(f, Xold, h)  #NewtonRaphson formula
   return Xnew

root = newraph(generateNthrootFunc(), 1, 0.000001)    # call the solver
print "The root is", root

The prompt for the input repeats several times... 输入提示重复多次...

That's becouse you're calling the function several times: 那是因为您多次调用该函数:

root = newraph(nthroot, 1, 0.000001)    # call the solver

end then inside newraph 然后在newraph结束

while (abs(Xold - Xnew) > h):  
  Xold = Xnew 
  Xnew = Xold - f(Xnew) / derivative(f, Xold, h)  #NewtonRaphson formula

where f is nthroot . 其中fnthroot

Then you call it agin in derivate (twice): 然后您将其称为derivate (两次):

def derivative(f, x, h):
  return (f(x+h) - f(x-h)) / (2.0*h)  

Posible solution 可能的解决方案

You could make nthroot a class 您可以将nthroot班级

class nthroot_generator:
    def __init__(self, number, root):
        self.number = number
        self.root = root
    def __call__(self, x):
        return x**(self.root) - (number)

and callit like this: 和这样的callit:

root = int(input("Please Enter a nth root (n) : "))
number =  int(input("Please Enter a number (x): "))
trueroot = newraph(nthroot_generator(root, number), 1, 0.000001)    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM