我有一个名为OpenAccount()
的函数,它接收用户的详细信息并将其附加到我的数据库字典中。在
我有一个数据库文件(模块),它被导入到我的函数文件中。在
我有一个名为appendRecord(key,**dictvalues)的函数,它将值附加到数据库文件中。在
但是,我不能在运行时调用函数并接受要附加的值。 它可以很好地与硬编码的值一起工作密码。请帮帮我。在
def AppendRecord(key, **dictvalues):
salesDept[key] = dict(dictvalues)
对硬编码值的要求是。。。在
^{pr2}$现在,当我试图从用户那里接收所有值和键时,我无法调用函数。我只是Python的初学者,因此请原谅我的错误:
编辑代码:
#!/usr/bin/python
import os #This module is imported so as to use clear function in the while-loop
import DB #Imports the data from database DB.py
def AppendRecord(key, **dictvalues):
DB.Accounts[key] = dict(dictvalues)
def OpenAccount(): #Opens a new a/c and appends to the database data-base.
while True:
os.system("clear") #Clears the screen once the loop is re-invoked
#Parameters taken from the user at Run-time so as to make entries in the database and append them
print '\n','Choose an Account Type'
print '\n','\n1)Savings Account','\n2)Current Account'
choice = input('Enter an optin: ')
if choice == 1:
name = raw_input('\nEnter a name: ')
depo = input('\nEnter amount(Rs.): ')
key = raw_input('\nEnter an alphanumeric-id: ')
acc= raw_input('\nEnter the Account-Type: ')
AppendRecord(key,Name=name,Initial_deposit=depo,Acctype=acc)
编辑1: 我得到的错误是。在
File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__
self.dict[key] = f.getvalue()
TypeError: 'int' object does not support item assignment
<2>编辑:
以下是数据库数据库文件源代码。在
#!/usr/bin/python
import shelve #Module:Shelve is imported to achieve persistence
Victor = {'Name':'Victor Hughes','Acctype':'Savings'}
Xavier = {'Name':'Xavier Bosco','Acctype':'Savings'}
Louis = {'Name':'Louis Philip','Acctype':'Current'}
Beverly = {'Name':'Beverly Dsilva','Acctype':'Current'}
Accounts = shelve.open('shelfile.shl') #Accounts = {}
Accounts['Louis']= Louis
Accounts['Beverly']= Beverly
Accounts['Xavier']= Xavier
Accounts['Victor']= Victor
Accounts.close()
您的问题是
DB
模块在您写入之前关闭了书架。去掉最后一行,它会很好的。在您还需要提供一个函数来关闭它或手动关闭它。在
现在需要在写入之前调用
DB.open_shelf()
函数,完成后再调用DB.close_shelf
函数。我建议你在课程开始和结束时分别给他们打电话。在您将注意到这实际上只是包装
shelve
并添加几乎为零的值。我将废弃整个DB
模块,直接使用shelve
。在相关问题 更多 >
编程相关推荐