我在我的代码中找不到错误,为什么我没有找到 a 和 b 的答案

I can not finde the Error in my code, and why i don´t become ich answer of a and b

有谁知道为什么我在我的代码中找不到错误?!如果您愿意,我将不胜感激,我是新手,正在努力学习,但我真的陷入了 python 的精髓之中!这是我得到的错误。 我需要将我在 Visual Basic 中的旧代码转换为 Python,当我这样做时,我没有从 a 或 b 得到答案。 这是我在 Visual Basic 中的代码

Public Class Form1
    Dim c As Double


    Private Function f(ByVal x As Double) As Double
        f = x * x - c

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a, b, eps, m As Double

        Dim n As Integer
        c = CDbl(TextBox1.Text)
        a = CDbl(TextBox2.Text)
        b = CDbl(TextBox3.Text)
        eps = CDbl(TextBox4.Text)


        If a > b Then
            MsgBox("Eingabe ist falsch")


        End If
        If a < 0 Or b < 0 Or c <= 0 Then
            MsgBox("falsche Eingabe")

        End If
        If a * a > c Or b * b < c Then
            MsgBox("Der Wert liegt nicht im Wertebereich")

        End If

        n = 0
        Do
            n = n + 1
            m = (a + b) / 2
            If f(m) > 0 Then
                b = m
            Else a = m


            End If
            eps = Math.Abs(a - b)

        Loop Until eps < Math.Pow(10, -13)


        TextBox5.Text = CStr(a)
        TextBox6.Text = CStr(b)



    End Sub
End Class


这是我在 Python 中的代码:


import math

def f(x):
    f=x**2-c

c=int(input("C= "))

a=int(input("a= "))

b= int(input("b= "))

eps=1e-13
print(eps)

#b muss größer als a sein
if a>b:
    print("Eingabe ist Falsch")
   

#alle Eingaben müssen größer als 0 sein
if a<0 or b<0 or c<=0:
    print ("Eingabe ist Falsch")

if a*a >c or b*b<c: 
    print("Eingabe ist Falsch")



n=0
while 0:
    n=n+1
    m=(a+b)/2
    if f(m)>0:
        b=m
    else:
        a=m
    eps=abs(a-b)

    if eps< math.pow(10,-13):
        break

    print ("a= ", a)
    print ("b= ", b)
    
    
print(a)
print(b)
    

这是您的代码的解决方案:

import math

def f(x):
    f=x**2-c
    return f #If you want to get the value of f(x), then you may return f

c=int(input("c= ")) #C= => c=
a=int(input("a= "))
b= int(input("b= "))
eps=float(input("eps= ")) #Ask the value of a float number
print(eps)

if a>b:
    print("Eingabe ist Falsch")
   
if a<0 or b<0 or c<=0:
    print ("falsche Eingabe")

if a*a>c or b*b<c: 
    print("Der Wert liegt nicht im Wertebereich")

n=0
while eps>=math.pow(10,-13): #It is the opposite condition, while there is this condition, the loop will continue
    n=n+1
    m=(a+b)/2
    if f(m)>0:
        b=m
    else:
        a=m
    eps=abs(a-b)
 
print(a)
print(b)

正如我所说,如果您想正确使用该函数,则必须 return 在函数末尾设置 f 的值。如果您的目标是循环直到特定条件,请记住进入循环的一种方法是只要您有相反的条件就执行循环。