<强>我使用以下代码打开的货架:

#!/usr/bin/python
import shelve                   #Module:Shelve is imported to achieve persistence

Accounts = 0
Victor = {'Name':'Victor Hughes','Email':'victor@yahoo.com','Deposit':65000,'Accno':'SA456178','Acctype':'Savings'}
Beverly = {'Name':'Beverly Dsilva','Email':'bevd@hotmail.com','Deposit':23000,'Accno':'CA432178','Acctype':'Current'}

def open_shelf(name='shelfile.shl'):
    global Accounts
    Accounts = shelve.open(name)          #Accounts = {}
    Accounts['Beverly']= Beverly    
    Accounts['Victor']= Victor


def close_shelf():
    Accounts.close()

我能够追加值的货架,但无法修改的值。 的我已经定义了一个函数存款(),从该我想修改数据存在于shelve.But它给我以下错误:

Traceback (most recent call last):
  File "./functest.py", line 16, in <module>
    Deposit()
  File "/home/pavitar/Software-Development/Python/Banking/Snippets/Depositfunc.py", line 18, in Deposit
    for key in Accounts:
TypeError: 'int' object is not iterable

<强>这是我的功能:

#!/usr/bin/python

import os                       #This module is imported so as to use clear function in the while-loop
from DB import *                       #Imports the data from database DB.py

def Deposit():
        while True:
                os.system("clear")              #Clears the screen once the loop is re-invoked
                input = raw_input('\nEnter the A/c type: ')
                flag=0
                for key in Accounts:
                        if Accounts[key]['Acctype'].find(input) != -1:
                                amt = input('\nAmount of Deposit: ')
                                flag+=1
                                Accounts[key]['Deposit'] += amt

                if flag == 0:
                        print "NO such Account!"    

if __name__ == '__main__':
        open_shelf()
        Deposit()
        close_shelf()

我是新来Python.Please help.Correct我,如果我wrong.I需要别人给的解释,这code.I'm的运作有点混淆。

有帮助吗?

解决方案

首先,不使用全球对Accounts,而通过来回。使用全球造成的错误。像这样:

def open_shelf(name='shelfile.shl'):
    Accounts = shelve.open(name)          #Accounts = {}
    ...
    return Accounts

def close_shelf(Accounts):
    Accounts.close()


def Deposit(Accounts):
    ...   

if __name__ == '__main__':
    Accounts = open_shelf()
    Deposit(Accounts)
    close_shelf(Accounts)

其次,不重新定义的内置函数。在Deposit(),你raw_input的结果分配给一个名为input变量:

input = raw_input('\nEnter the A/c type: ')

四大行后,您尝试使用内置的input功能:

amt = input('\nAmount of Deposit: ')

但是,这不会工作,因为input已经被重新定义!

第三,当迭代过搁置物品,按照1)抓斗搁置物品的图案,2)发生变异项,3)写突变项回搁板。像这样:

for key, acct in Accounts.iteritems():  # grab a shelved item
    if val['Acctype'].find(input) != -1:
        amt = input('\nAmount of Deposit: ')
        flag+=1
        acct['Deposit'] += amt          # mutate the item
        Accounts[key] = acct            # write item back to shelf

(建议的该第三位是从 hughdbrown 的回答微调。)

其他提示

我觉得你有更多的运气是这样的:

for key, val in Accounts.iteritems():
    if val['Acctype'].find(input) != -1:
        amt = input('\nAmount of Deposit: ')
        flag+=1
        val['Deposit'] += amt
        Accounts[key] = val
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top