은 오일러의 방법에서 내 시도, 나는 다음과 같은 오류가 나타납니다파이썬 : 번호와 (오일러의 방법) 작동하지 운영자 연결 나는 다음 (파이썬) 코드를 실행할 때마다
y += h * eval(diff_eq_solved); TypeError: cannot concatenate 'str' and 'float' objects.
오류가 이해하기 쉬운을 ,하지만 분할의 모든 항목이 문자열로 변환 되었기 때문에 어디에 문제가 있는지 확실하지 않습니다. 내 생각 엔 + 연산자가 문자열로 포함되어 있지 않지만이 문제를 해결하는 방법을 모르겠습니다 (어쩌면 이스케이프?). 도와주세요!
x = raw_input("What is your initial x? ")
y = raw_input("What is your initial y? ")
h = float(raw_input("What is your step size? "))
final_x = float(raw_input("At what value of x would you like to approximate the solution? "))
diff_eq = raw_input("What is your differential equation? ")
split = list(diff_eq)
while float(x) < final_x:
def replace(split, X, Y):
i = 0
for v in split:
if v == X:
split.pop(i)
split.insert(i, Y)
i += 1
replace(split, "x", str(x))
replace(split, "y", str(y))
diff_eq_solved = ''.join(split)
y += h * eval(diff_eq_solved)
x += h
if type(y) != int:
print "Syntax Error."
print y
이 오류를 얻을 수있는 프로그램에 전달할 무엇을 입력? – twasbrillig
나는 그 질문에 그것을 포함해야했다! -_- 바보 나 .. X = 0, Y = 1 H = 0.5 final_x = 6 diff_eq = X + Y – astroball
I 짐작 X = 1, Y = 1 H = 1 = 1 최종 DIFF = X + y ... 그게 충분히 잘 작동했습니다 :) – GreenAsJade