2017-04-14 1 views
-1

입력 된 수학 함수의 근원을 찾는 프로그램을 작성하려고했습니다. 저는 방금 시작 했으므로 여기에 나와있는 것은 시작에 불과하며 사용되지 않는 변수가 있습니다. 여기.join 함수에 익숙하지 않은 오류

내가 용어 값을 사용하면 여기에 입력, 말, (100)와 함수에 'X'를 대체 할 것으로 예상되는 함수를 쓴 코드 :

code = list(input("Enter mathematical function: ")) 
lowBound = int(input("Enter lower bound: ")) 
upBound = int(input("Enter upper bound: ")) 

def plugin(myList, value): 
    for i in range(len(myList)): 
    if myList[i] == 'x': 
     myList[i] = value #replaces x with the inputted value 
    return ''.join(myList) #supposed to turn the list of characters back into a string 

print(plugin(code,upBound)) 

하지만 프로그램을 실행할 때 나는 오류 얻을 :

Traceback (most recent call last): 
File "python", line 11, in <module> 
File "python", line 9, in plugin 
TypeError: sequence item 0: expected str instance, int found 

(I 온라인 프로그래밍 플랫폼을 사용하고 있습니다를, 그래서 파일은 단지 '파이썬'라고 함)이 나에게 어떤 이해가되지 않습니다

. myList는 int가 아니어야하며 올바른 데이터 유형 (str) 인 경우에도 목록이어야합니다. 누군가 여기서 일어나는 일을 설명 할 수 있습니까?

+3

'upBound'는 정수입니다. 목록에 넣으면됩니다. 'str.join()'을 사용하여 문자열 값 이외의 것을 결합 할 수 없습니다. –

답변

1

st 유형 (또는 문자)을 int 유형으로 바꿉니다.

대신을 시도해보십시오

myList[i] = str(value) 
0

당신은 문자열을 더 간결하게

return ''.join(str(x) for x in myList) 

또는의 반복자에 가입하실 수 있습니다. 함수를 제거하십시오.

print(''.join(str(upBound if x =='x' else x) for x in code) 
관련 문제