所以我用問題輸入了多個變數。我想出了如何打開文本檔案,但如何將所有變數保存到我的文本檔案中。它在文本檔案中一直沒有回傳
from distutils import text_file
kids = input("How many kids do you have?>")
food = input("Whats your favorite food?>")
tv = input("Whats your favorite sports team>")
work = input("Where do you work?>")
carmake = input("Whats the make of your car?>")
symbol = input("Pick a symbol !@#$%^&*()_ ")
results = str(print(f"{kids}, {food}, {tv}, {work}, {carmake}, {symbol}".split(" ")))
file = open("passwordGenerator.txt", "w")
file.write(results)
file.close
uj5u.com熱心網友回復:
我會在一個串列中實體化不同的變數,然后用一個連接來連接字串(以確保每個單詞之間有逗號和空格,就像你原來的例子一樣)。
為了確保檔案被干凈地處理,我建議使用背景關系管理器來打開和寫入檔案。
input_array = [
input("How many kids do you have?>")
,input("Whats your favorite food?>")
,input("Whats your favorite sports team>")
,input("Where do you work?>")
,input("Whats the make of your car?>")
,input("Pick a symbol !@#$%^&*()_ ")
]
results = ", ".join(input_array)
with open("password_generator.txt", "w") as file:
file.write(results)
uj5u.com熱心網友回復:
result變數是一個print函式,執行None后回傳。
此外,from distutils import text_file是未使用的匯入。您可以洗掉此行,因為它什么都不做。
代碼在這里:
kids = input("How many kids do you have?>")
food = input("Whats your favorite food?>")
tv = input("Whats your favorite sports team>")
work = input("Where do you work?>")
carmake = input("Whats the make of your car?>")
symbol = input("Pick a symbol !@#$%^&*()_ ")
results = kids food tv work carmake symbol
print(results)
file = open("passwordGenerator.txt", "w")
file.write(results)
file.close()
uj5u.com熱心網友回復:
您想使用 連接不同的字串 。并且print不是必需的,也不是split。
kids = input("How many kids do you have?>")
food = input("Whats your favorite food?>")
tv = input("Whats your favorite sports team>")
work = input("Where do you work?>")
carmake = input("Whats the make of your car?>")
symbol = input("Pick a symbol !@#$%^&*()_ ")
results = kids food tv work carmake symbol
with open("password_generator.txt", "w") as file:
file.write(results)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494926.html
上一篇:將新物件附加到單獨變數中的串列
