-1
所以我有這個項目運行良好,唯一的問題是我得到的文件內寫入的返回值。這裏是我的代碼:如何在寫入模式下使用「返回值」
def write_substrings_to_file(s,filename):
if type(s) != str:
raise TypeError ("You have entered something other than a sting, please enter a string next time!")
if s=="" or filename=="":
raise ValueError
why=open(filename,"wt")
output=""
if len(s)==1:
return s[0]
for i in range(0,len(s)):
for n in range(0,len(s)):
output+=s[i:n+1]
break
return output+write_substrings_to_file(s[1:],filename)
why.write()
why.close()
在我需要的最後三行是
return output+write_substrings_to_file(s[1:],filename)
why.write(return)
why.close()
但我不能使用這樣的方式回報換句話說,我收到以下錯誤
TypeError: cannot concatenate 'str' and 'type' objects
你能解釋一下函數應該做什麼嗎? – mitoRibo
讓我們說輸入的是 'ABC' 打開一個文件,寫上如下: 一個 AB ABC b BC Ç 另一個解釋,輸入:大 摹 克 GRE 格雷亞 大 [R 重新 意圖 不動產資產信託 Ë EA 吃 一個 在 牛逼 ,然後保存該文件,另一件事是h作爲遞歸函數 – Enigma