2016-09-10 77 views
-4
foo = ("PandaBears") 
l = list(foo) 
random.shuffle(l) 
Output = ''.join(l) 
print(Output) 

我一直在這個代碼試圖找出問題的年齡,但我沒有運氣。幾個小時它完全沒有麻煩完美的工作之前 - I haven't even changed/upgraded python either.TypeError:列表標記必須是整數,而不是str

錯誤就要從

l = list(idf)

和我一直在使用[]代替()試過。

在這段代碼的任何改進,將不勝感激

+0

該代碼表示​​l = list(foo)。但我看到l = list(idf)。什麼是idf? –

+0

你正試圖洗牌字符串的字符? –

+0

發佈的代碼正確地洗牌字符串的字符。請確保您確認代碼重現錯誤。 –

回答

1

括號在這裏沒用:

foo = ("PandaBears") 

只要寫:

foo = "PandaBears" # str 

如果你想一個單身tuple,添加一個尾隨逗號:

foo = ("PandaBears",) # tuple 

如果foo是一個字符串,下面的語句構造的字母列表:

l = list(foo) 

不知道什麼是diff

相關問題