读写无限的名称到文本文件

时间:2016-11-12 16:16:23

标签: python-3.x

据我所知http://pastebin.com/mfJfp5sB我一次只能存储一个名字。我想知道如何创建一个输入所有姓名的字典,并检查之前是否输入了该人的姓名。

    blue = "\033[94m"

name = input("What's your name?")

with open("name.txt") as openfile:
    for line in openfile:
        for part in line.split():
            if name in part:
                    print(blue + "I remember you " + name + "!")
            else:
                print("Well Nice to meet you " + name + "!")

file = open("name.txt", "w")
file.write(name)
file.close()

1 个答案:

答案 0 :(得分:0)

如果可以告诉我,请尝试以下操作:

name = input("What's your name?")
with open("name.txt", 'a') as openfile:
     openfile.write(name)
with open("name.txt",'r+') as openfile:
    for line in openfile:
        for part in line.split():
            if name in part:
                    print(blue + "I remember you " + name + "!")
            else:
                print("Well Nice to meet you " + name + "!")