私はGCSEコンピュータサイエンスを勉強している年10人の学生として、私はちょっと新しいpythonです。私はバブルソートアルゴリズムをコーディングしようとしています。TypeError: Can't convert 'int' object to str implicitly
につまずいています。x
とlength
の両方をチェックしたところ、彼らは両方とも整数です。誰か助けて! :)ここで'int'を文字列に変換できません
は、これまでの私のコードです:
x = 1
list1 = list(input("What numbers need sorting? Enter them as all one - "))
length = len(list1)
print(list1)
while True:
for i in range(0,length):
try:
if list1[i] > list1[i+1]:
x = list1[i]
list1.remove(x)
list1.insert(i+1,x)
print(list1)
if list1[i] < list1[i+1]:
x += 1
print(list1)
except IndexError:
break
if x == length:
print("The sorted list is - ",''.join(list1))
break
それを反復しながら、リストを変更しないでください。さらに、ここで 'x'はソートされたインスタンスを数え、要素を入れ替えるという2つの目的で使用します。 –
'x'に文字列値を代入しようとしています:' x = list1 [i] '。リスト 'list1'は、整数ではなく文字列のリストです。 –
このコードはpython 3で正しく動作します。このcodをpython2で使用していますか? –