3

次のコードを使用して棚を開きました。

#!/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()

シェルフに値を追加することはできますが、値を変更することはできません。 シェルフに存在するデータを変更したい関数Deposit()を定義しましたが、次のエラーが発生します:

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は初めてです。助けてください。間違っている場合は訂正してください。このコードの機能について少し説明してもらう必要があります。混乱しています。

4

2 に答える 2

4

まず、 にグローバルを使用しないでください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: ')

4 行後、組み込み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

(この 3 番目のアドバイスは、huhdbrownの回答から微調整されました。)

于 2010-10-25T20:13:17.387 に答える
3

次のような幸運が訪れると思います。

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
于 2010-10-25T19:07:55.007 に答える