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

Time:09-21

Does anyone know why I can not find an error in my code ?! I would really appreciate it if you do, I am new to this and trying to learn but im getting really caught up in the nitty gritty of python! This is the error I am getting. I need to convert my old code in Visual Basic into Python, and when i did it, I do not get the Answer from a or b. This is my code in 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


and here is my code in 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)
    

CodePudding user response:

Here is the solution to your code:

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)

As I said, if you want to use the function properly, then you will have to return the value of f at the end of your function. If your goal is to have a loop until a specific condition, then keep in mind that a way to enter in the loop is to do it as long as you have the opposite condition.

  • Related