我正在做一些python 3.5的在線教程,並學習瞭如何將字符串寫入文件並保存。現在我正在嘗試寫一個函數的內容,但沒有任何運氣。Python,將函數的內容寫入.txt文件
from RECAPS import recap3_1
recap3_1.get_details("Mark", "M", 26)
recap3_1.get_schooling(30000,260,900)
fw = open("Details.txt", "w")
fw.write(recap3_1.get_details("Mark", "M", 26))
fw.close()
然後我得到這個錯誤
Mark Male 26
Traceback (most recent call last):
29360
File "E:/PyProjects/YoutubeProject/RECAPS/recap3.py", line 8, in <module>
fw.write(recap3_1.get_details("Mark", "M", 26))
Mark Male 26
TypeError: write() argument must be str, not None
Process finished with exit code 1
的get_details功能僅僅是這一點,
def get_details(name, gender, age=0):
if age < 18:
print("You are too young to register")
return
if len(name) < 2:
print("Looks like your name is too short")
return
if gender is "M":
gender = "Male"
elif gender is "F":
gender = "Female"
else:
gender = "Unknown"
print(name, gender, age)
我在想什麼。我希望從另一個.py文件抓取功能並保存內容
get_details是做什麼的?你需要粘貼更多代碼 – haifzhan
@HaifengZhang –
如果'get_details'沒有返回一個字符串,而是'None'(也就是沒有),那麼顯然沒有什麼要寫入文件的。你的問題可能是如何從函數返回值,而不是如何寫入文件。 – Evert