2017-04-14 6 views
-1

私は、入力された数学関数の根を見つけるプログラムを作成しようとしています。私はちょうど始めたばかりなので、私がここに示したのは始まりに過ぎず、未使用の変数があります。.join関数の見知らぬエラー

ここで私はあなたの入力した値と関数内で用語「X」を交換することになっている機能を書いて、たとえば、ここで100はコードです:

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 

(私はオンラインプログラミングプラットフォームを使用していますので、ファイルは単に「のpython」と呼ばれる)

これは、私にはどんな意味がありません。 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) 
関連する問題