Reputation: 51
I'm making a chatbot and the first thing the user does if the UserName.txt file isn't there is input their name and likes, but the inputted text doesn't save. What am I doing wrong?
I've tried removing the UserLike function to see if that makes a difference, but it doesn't seem to work
def NameSay():
UserName = input(": ")
UserNameFile = open("UserName.txt", "w+")
UserNameFile.write(UserName)
print("So your name is "+ UserName + ", right?")
NameConfirm = input(": ")
if 'yes' in NameConfirm or 'Yes' in NameConfirm or 'right' in NameConfirm or 'Right' in NameConfirm or 'ya' in NameConfirm or 'Ya' in NameConfirm or 'yeah' in NameConfirm or 'Yeah' in NameConfirm:
print("Good to meet you, " + UserName + ". I'm Ene, your virtual assistant, friend, coworker, whatever you need me to be!")
print("Now, why don't you tell me a bit about yourself? What you like and all of that.")
UserLike = input(": ")
UserLikesFile = open("UserLikes.txt", "w")
UserLikesFile.write(UserLike)
print("Thank you! This is very interesting info.")
if NameConfirm in ['no', 'No']:
print("Oh? Then tell me what your name is.")
NameSay()
if os.path.isfile('UserName.txt') == True:
f = open("UserName.txt", "r")
file_contents = f.read()
welcomes = ["Welcome back, " + file_contents, "Hey-o! Good to see you again, " + file_contents]
print("\n" + welcomes[random.randint(0,1)])
Main_Menu()
if os.path.isfile('UserName.txt') == False:
print("\nHey-o! I don't think we've met before! What\'s your name?")
NameSay()
The text should save into the written files, but the files end up being blank.
Upvotes: 0
Views: 57
Reputation: 633
Your code has two problem I think:
1- When you open a file, you should close or flush that file to be able to read that file. I used with
statement for file management.
2- You are calling NameSay()
recursively and if a person enter his username wrong for 10 times, your function call stack should save all of them. So, I edited your last line of code that doesn't call the function recursively and edited the NameSay
function to return True
or False
for determine that was it successful or not
def NameSay():
UserName = input(": ")
with open("UserName.txt", "w+") as UserNameFile:
UserNameFile.write(UserName)
print("So your name is "+ UserName + ", right?")
NameConfirm = input(": ")
if 'yes' in NameConfirm or 'Yes' in NameConfirm or 'right' in NameConfirm or 'Right' in NameConfirm or 'ya' in NameConfirm or 'Ya' in NameConfirm or 'yeah' in NameConfirm or 'Yeah' in NameConfirm:
print("Good to meet you, " + UserName + ". I'm Ene, your virtual assistant, friend, coworker, whatever you need me to be!")
print("Now, why don't you tell me a bit about yourself? What you like and all of that.")
UserLike = input(": ")
with open("UserLikes.txt", "w") as UserLikesFile:
UserLikesFile.write(UserLike)
print("Thank you! This is very interesting info.")
return True
if NameConfirm in ['no', 'No']:
print("Oh? Then tell me what your name is.")
return False
if os.path.isfile('UserName.txt') == True:
f = open("UserName.txt", "r")
file_contents = f.read()
welcomes = ["Welcome back, " + file_contents, "Hey-o! Good to see you again, " + file_contents]
print("\n" + welcomes[random.randint(0,1)])
Main_Menu()
if os.path.isfile('UserName.txt') == False:
print("\nHey-o! I don't think we've met before! What\'s your name?")
while(not NameSay())
continue
Upvotes: 0
Reputation: 1769
To be on the safe side, you can flush the output to the file right after the write function.
UserNameFile.flush()
Also, don't forget to close the the file
UserNameFile.close()
Upvotes: 1
Reputation: 85
I've have just ran this and the data did save so I'm not too sure what you are on about. However I can see you haven't closed the files after using them as you should.
Upvotes: 1