簡體   English   中英

為什么我寫的文件沒有把輸入的文字保存在Python中?

[英]Why don't my written files not save the inputted text in Python?

我正在制作一個聊天機器人,如果沒有 UserName.txt 文件,用戶要做的第一件事就是輸入他們的名字和喜歡的內容,但輸入的文本不會保存。 我究竟做錯了什么?

我試過刪除 UserLike function 看看是否有區別,但它似乎不起作用

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()

文本應保存到書面文件中,但文件最終為空白。

我剛剛運行了這個,數據確實保存了,所以我不太確定你在說什么。 但是,我可以看到您在使用它們后沒有關閉文件。

為了安全起見,您可以在寫入 function 之后立即將 output 刷新到文件中。

UserNameFile.flush()

另外,不要忘記關閉文件

UserNameFile.close()

我認為您的代碼有兩個問題:

1-當您打開一個文件時,您應該關閉或刷新該文件以便能夠讀取該文件。 我使用with語句進行文件管理。

2-您正在遞歸調用NameSay()並且如果一個人輸入他的用戶名錯誤 10 次,您的 function 調用堆棧應該保存所有這些。 所以,我編輯了你最后一行不遞歸調用 function 的代碼並編輯了NameSay function 以返回TrueFalse以確定它是否成功

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM