Come possiamo modificare i dati che si trova in uno scaffale?
-
26-09-2019 - |
Domanda
Ho aperto un ripiano utilizzando il seguente codice:
#!/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()
Sono in grado di aggiungere valori al ripiano, ma in grado di modificare i valori. Ho definito una funzione Deposito () da cui vorrei modificare i dati presenti nel shelve.But mi dà il seguente errore:
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
Ecco la mia funzione:
#!/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()
Sono nuovo di Python.Please me help.Correct se sono wrong.I bisogno di qualcuno per dare un po 'di spiegazione del funzionamento di questo code.I'm confuso.
Soluzione
In primo luogo, non utilizzare un globale per Accounts
, piuttosto passare avanti e indietro. Utilizzando il globale causato l'errore. In questo modo:
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)
In secondo luogo, non redefine built-in funzioni. In Deposit()
, si assegna il risultato di raw_input
a un nome input
variabile:
input = raw_input('\nEnter the A/c type: ')
linee Quattro tardi, si tenta di utilizzare la funzione built-in input
:
amt = input('\nAmount of Deposit: ')
Ma questo non funzionerà perché input
è stato ridefinito!
In terzo luogo, quando l'iterazione elementi sopra accantonato, seguono il modello del 1) afferrare punto accantonato, 2) voce di mutare, 3) mutato articolo di nuovo a scaffale. In questo modo:
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
(Questo terzo po 'di consigli è stato ottimizzato da hughdbrown ' s risposta.)
Altri suggerimenti
Credo che avresti avuto più fortuna in questo modo:
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