Python 尝试调用main中的函数,但未获得2个输出

Python 尝试调用main中的函数,但未获得2个输出,python,function,math,syntax-error,Python,Function,Math,Syntax Error,我有一个家庭作业问题,我将在下面展示,那就是要求我在主函数中封装一个平方根函数。如果用户点击enter键,它将退出循环,否则它将继续运行。我在这里遇到了两个问题,第一个问题是,每当我运行它时,我总是得到两个输出,我知道这是为什么,但我似乎无法绕过它。第二个问题是,我不知道如何让用户在不将所有内容转换为字符串的情况下按enter键并退出程序 import math def newton(): x = float(input("Enter a positive number: "))

我有一个家庭作业问题,我将在下面展示,那就是要求我在主函数中封装一个平方根函数。如果用户点击enter键,它将退出循环,否则它将继续运行。我在这里遇到了两个问题,第一个问题是,每当我运行它时,我总是得到两个输出,我知道这是为什么,但我似乎无法绕过它。第二个问题是,我不知道如何让用户在不将所有内容转换为字符串的情况下按enter键并退出程序

import math

def newton():
     x = float(input("Enter a positive number: "))

     tolerance = 0.000001
     estimate = 1.0

     while True:
          estimate = (estimate + x / estimate) / 2
          difference = abs(x - estimate ** 2)
          if difference <= tolerance:
              break

     print("The program's estimate is", estimate)
     print("Python's estimate is     ", math.sqrt(x))

def main():
     x = str(input("Enter a positive number: "))
     if x == '':
          return
     else:
          x = float(x)
          newton()
main()
def牛顿(x):
公差=0.000001
估计值=1.0
尽管如此:
估算=(估算+x/估算)/2
差值=绝对值(x-估计值**2)

如果差异,只需请求输入一次,然后将结果传递给牛顿函数。为了保持循环,我在main中添加了一个循环,它将继续请求输入。如果输入可以转换为浮点,它将调用newton;如果不能转换,它将停止

import math

def newton(number):
     tolerance = 0.000001
     estimate = 1.0

     while True:
          estimate = (estimate + number / estimate) / 2
          difference = abs(number - estimate ** 2)
          if difference <= tolerance:
              break

     print("The program's estimate is", estimate)
     print("Python's estimate is     ", math.sqrt(number))

def main():
    while True:
        x = str(input("Enter a positive number: "))
        try:
            x = float(x)
            newton(x)
        except ValueError as ve:
            break
main()
导入数学
def牛顿(编号):
公差=0.000001
估计值=1.0
尽管如此:
估算=(估算+数量/估算)/2
差异=绝对值(数量-估算**2)

如果您要求输入两次。一次在主要领域,一次在牛顿领域,你说“我总是得到两个输出”是什么意思?你有两个prints命令,所以它会打印两行。我知道,但我不确定如何解决这个问题。只需使用
newton()
启动
main()
def newton(x):
    tolerance = 0.000001
    estimate = 1.0

    while True:
        estimate = (estimate + x / estimate) / 2
        difference = abs(x - estimate ** 2)
        if difference <= tolerance:
            break

    print("The program's estimate is", estimate)
    print("Python's estimate is     ", math.sqrt(x))

 def main():
     x = str(input("Enter a positive number: "))
     if x == '':
         return
     else:
         x = float(x)
         newton(x)
import math

def newton(number):
     tolerance = 0.000001
     estimate = 1.0

     while True:
          estimate = (estimate + number / estimate) / 2
          difference = abs(number - estimate ** 2)
          if difference <= tolerance:
              break

     print("The program's estimate is", estimate)
     print("Python's estimate is     ", math.sqrt(number))

def main():
    while True:
        x = str(input("Enter a positive number: "))
        try:
            x = float(x)
            newton(x)
        except ValueError as ve:
            break
main()