我正在编写一些代码来学习如何更好地使用类。在Python中学习了关于持久性和书架的知识。 我试着让我的用户输入一些东西,然后这些东西被用来作为我唯一的类的一个对象。在
import shelve
filearch = shelve.open('archive')
filearch['patients'] = {}
class Patient():
def __init__(self, name='', surname='', notes=''):
self.name = name
self.surname = surname
self.notes = notes
def makeone():
print('Insert a name')
nome = input('Nome: ')
print('Insert a surname')
cognome = input('surname: ')
print('Insert notes')
data = input('notes: ')
a = {'name': name, 'surname': surname, 'notes': notes}
return a
def addone():
users_pat = Patient(**makeone())
return users_pat
def save(user_paz):
return filearch['patients'].update({user_paz : a)
有人能解释一下我做错了什么吗?在
在这段代码中有一些东西需要修正。在
首先,它没有运行,因为这行是无效的,它缺少一个结束的'}'。在
一旦这个问题得到解决,像这样运行代码
^{pr2}$导致此错误:
^{3}${{{cd6}>和的值都是的。在
如果我们修复这些名称并再次运行,我们将得到:
In
a
是在makeone
中创建的变量的名称,但该名称仅在makeone
函数内有效。save
函数只知道传递给它的user_paz
变量,因此请更改到
最后,如注释中所述,您需要关闭搁置文件以确保内容已保存。在
下面是代码的改进版本:它保存输入,然后报告搁置文件的内容。在
相关问题 更多 >
编程相关推荐