So I made multiple variables with questions to input. I figured out how to open the text file but how do I save all of my variables to my text file. It keep returning none in the text file
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
CodePudding user response:
I'd instantiate the different variables in a list and then concatenate the strings with a join (to make sure there's the comma and space between each word as in your original example).
To make sure that the file is handled cleanly, I'd suggest using a context manager to open and write to the file.
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)
CodePudding user response:
The result
variable is a print
function, which returns None
after executing.
Also, from distutils import text_file
is a unused import. You can delete this line because it does nothing.
Code here:
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()
CodePudding user response:
You want to concatenate different strings using
. And print
is not required for this, nor is 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)