Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:31:06+00:00 2026-05-17T22:31:06+00:00

I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is

  • 0

I have opened a shelve using the following code:

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

I am able to append values to the shelve but unable to modify the values.
I have defined a function Deposit() from which I would like to modify the data present in the shelve.But it gives me the following error:

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

Here is my Function:

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

I’m new to Python.Please help.Correct me if I’m wrong.I need someone to give a little bit of explanation as to the functioning of this code.I’m confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:31:06+00:00Added an answer on May 17, 2026 at 10:31 pm

    Firstly, don’t use a global for Accounts, rather pass it back and forth. Using the global caused your error. Like this:

    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)
    

    Secondly, don’t redefine built-in functions. In Deposit(), you assign the result of raw_input to a variable named input:

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

    Four lines later, you try to use the built-in input function:

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

    But that won’t work because input has been redefined!

    Thirdly, when iterating over shelved items, follow the pattern of 1) grab shelved item, 2) mutate item, 3) write mutated item back to shelf. Like so:

    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
    

    (This third bit of advice was tweaked from hughdbrown‘s answer.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have opened the view template as popup by using the following code: <%=
i have opened fancy box with following code $(#tip5).fancybox({ 'width': 400, 'height': 370, 'enableEscapeButton'
I have opened one file with following way: fp = fopen(some.txt,r); Now in this
I have an application(Developed using Adobe AIR) in which i have opened a port
On a typical OS how many files can i have opened at once using
I have a document that I have opened in notepad++ that has the following
I have opened a large excel file as a Apache POI workbook (using VM
I have opened a .txt file many times using the C language's file handling
I have opened a website using WebBrowser. Now I would like to programmatically click
I am making the qr-code reader app,on button click i have opened the camera

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.