如何让Python脚本保存输入?

时间:2015-11-10 02:02:32

标签: python

如何将此Python转换为更新已知“生日?”的内容。

我想要它,所以当我输入一个输入时,它会自动保存,所以我可以参考再次输入的“生日”。

    birthdays = {'Doge1': 'Apr 1', 'Doge2': 'Dec 12', 'Doge3': 'Mar 4'}
     while True:
     print('Enter a name: (blank to quit)')
     name = input()
      if name == '':
       break
        if name in birthdays:
        print(birthdays[name] + ' is the birthday of ' + name)
       else:
       print('I do not have birthday information for ' + name)
       print('What is their birthday?')
       bday = input()
       birthdays[name] = bday
       print('Birthday database updated.')

2 个答案:

答案 0 :(得分:1)

您需要选择存储选项。如果您想将任何对象存储在文件中,可以使用Python的cPickle库。如果您打算存储大量数据,我当然建议使用数据库。你可以从SQLite3开始。

答案 1 :(得分:0)

print(birthdays[name] + ' is the birthday of ' + name)之后,插入:

print('Would you like to update his/her birthday? (y/n)')
update = input()
if update == 'y':
    print('What is his/her birthday?')
    newBirthday = input()
    birthdays[name] = newBirthday
    print('Birthday database updated.')
elif update == 'n':
    break
else:
    print('Invalid Input')