有沒有人知道為什么我的代碼中找不到錯誤?如果你知道的話,我將非常感激,我是個新手,正在努力學習,但我真的被Python的細枝末節所困擾。這就是我遇到的錯誤。 我需要將Visual Basic中的舊代碼轉換為Python,當我這樣做時,我沒有得到a或b的答案。 這是我在Visual Basic中的代碼
Public Class Form1
Dim c AsDouble
Private Function f(ByVal x As Double) As Double Double
f = x * x - c
結束 函式[/span
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1. 點擊
Dim a, b, eps, mAs Double
Dim n As Integer[/span
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")
結束 若
如果 a * a > c Or b * b < c Then
MsgBox("Der Wert liegt nicht im Wertebereich"/span>)
End If
n = 0
做
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)
結束 Sub
結束 類別
下面是我在Python中的代碼:
import math
def f(x)。
f=x**2-c
c=int(input("C="/span>)
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"/span>)
#alle Eingaben müssen gr??er als 0 sein。
if a<0 or b< 0 or c<=0:
print ("Eingabe ist Falsch"/span>)
if a*a >c or b*b<c:
print("Eingabe ist Falsch"/span>)
n=0。
while 0:
n=n 1: n=n 1.
m=(a b)/2: n=n 1
if f(m)>0:
b=m
else:
a=m
eps=abs(a-b)
if eps< math.pow(10,-13)。
break: break?
print ("a="/span>, a)
print ("b="/span>, b)
print(a)
print(b)
uj5u.com熱心網友回復:
下面是你的代碼的解決方案:
import math
def f(x)。
f=x**2-c
return f #如果你想得到f(x)的值,那么你可以回傳f。
c=int(input("c="/span>) #C= => c=
a=int(input("a=")
b= int(input("b=")
eps=float(input("eps=")) #Ask the value of a float number(specialty)
print(eps)
if a>b:
print("Eingabe ist Falsch"/span>)
if a<0 or b< 0 or c<=0:
print ("falsche Eingabe"/span>)
if a*a>c or b*b<c:
print("Der Wert liegt nicht im Wertebereich"/span>)
n=0
while eps>=math.pow(10,-13)。#It is 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)
正如我所說的,如果你想正確地使用這個函式,那么你就必須在函式的最后回傳f的值。如果你的目標是要有一個回圈,直到一個特定的條件,那么請記住,進入回圈的一個方法是只要你有相反的條件就可以了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/325296.html
標籤:
下一篇:同時運行兩個任務
