2014-11-26 1 views
1

은 오일러의 방법에서 내 시도, 나는 다음과 같은 오류가 나타납니다파이썬 : 번호와 (오일러의 방법) 작동하지 운영자 연결 나는 다음 (파이썬) 코드를 실행할 때마다

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 
+0

이 오류를 얻을 수있는 프로그램에 전달할 무엇을 입력? – twasbrillig

+0

나는 그 질문에 그것을 포함해야했다! -_- 바보 나 .. X = 0, Y = 1 H = 0.5 final_x = 6 diff_eq = X + Y – astroball

+0

I 짐작 X = 1, Y = 1 H = 1 = 1 최종 DIFF = X + y ... 그게 충분히 잘 작동했습니다 :) – GreenAsJade

답변

0

당신은 자신을 수행하여이 질문에 대답 할 수 있습니다 : 당신은 Y의 종류가 str 것을 발견 할 것이다

print type(y), type(h), type(eval(diff_eq_solved))

- raw_input을의 문서는

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

을 말한다. .. 그래서 float로 변환해야합니다 :

y=float(raw_input(prompt))

결과 프로그램은 다음과 같습니다

x = float(raw_input("What is your initial x? ")) 

y = float(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) 
    print type(y), type(h), type(eval(diff_eq_solved)) 
    y += h * eval(diff_eq_solved) 
    x += h 

if type(y) != int: 
    print "Syntax Error." 

print y 

결과 출력은 다음과 같습니다

(h2hh)~ mgregory$ python foo.py 
What is your initial x? 0 
What is your initial y? 1 
What is your step size? 0.5 
At what value of x would you like to approximate the solution? 6 
What is your differential equation? x*y 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
<type 'float'> <type 'float'> <type 'float'> 
Syntax Error. 
1.0 
(h2hh)~ mgregory$ 
+0

흠 ... 작동하지 않는 것 같아요 :(나는 또한 x = float (raw_input())) 또한 같은 오류가 발생했습니다. – astroball

+0

저에게 맞습니다. 프로그램 및 결과가 추가되었습니다. – GreenAsJade

+0

나는 내 실수를 발견했다. 비슷한 이름의 오래된 파일을 실행하고있는 것처럼 보입니다. 도움과 인내심에 감사드립니다 !! – astroball

관련 문제