我正在创建一个User类,在这个类中我想创建一个提示用户输入数据的方法。如何在populate()方法中使用 init 参数作为有效值?
这是我的代码:
class User:
def __init__(self, last_name, first_name, social, rep_code, data_source):
self.last_name = last_name
self.first_name = first_name
self.social = social
self.rep_code = rep_code
self.data_source = data_source
def populate(self):
#prompt for user info and write to file
this.last_name = input("Please enter user last name:\n")
this.first_name = input("Please enter user first name:\n")
this.social = input("Please enter user social security number:\n")
this.rep_code = input("Please enter user rep code:\n")
this.data_source = input("Please choose the Data Source. Enter 6 for
NFS, 1 for DST, 2 for DAZL:\n")
我将此方法作为user1.populate()
运行谢谢。
答案 0 :(得分:0)
这是你在找什么?
class User:
def __init__(self, last_name, first_name, social, rep_code, data_source):
self.last_name = last_name
self.first_name = first_name
self.social = social
self.rep_code = rep_code
self.data_source = data_source
@staticmethod
def create():
last_name = input("Please enter user last name:\n")
first_name = input("Please enter user first name:\n")
social = input("Please enter user social security number:\n")
rep_code = input("Please enter user rep code:\n")
data_source = input("Please choose the Data Source. Enter 6 for NFS, 1 for DST, 2 for DAZL:\n")
return User(last_name, first_name, social, rep_code, data_source)
此代码允许您调用User.create()
来提示问题和return
User
对象。这与this
替换为self
的代码不同,因为这不需要现有的User
对象,而是创建一个新对象。
用法:
如何执行此操作的示例可能是这样的。
usr = User.create()
print("First name: " + usr.first_name)
print("Last name: " + usr.last_name)
答案 1 :(得分:0)
我会把查询放在课外:
def query():
attributes = {}
#prompt for user info and write to file
attributes['last_name'] = input("Please enter user last name:\n")
attributes['first_name'] = input("Please enter user first name:\n")
attributes['social'] = input("Please enter user social security number:\n")
attributes['rep_code'] = input("Please enter user rep code:\n")
attributes['data_source'] = input("Please choose the Data Source. Enter 6 for NFS, 1 for DST, 2 for DAZL:\n")
return attributes
班级看起来像这样;
class User:
def __init__(self, last_name, first_name, social, rep_code, data_source):
self.last_name = last_name
self.first_name = first_name
self.social = social
self.rep_code = rep_code
self.data_source = data_source
用法:
>>> attrs = query()
Please enter user last name:
q
Please enter user first name:
w
Please enter user social security number:
e
Please enter user rep code:
r
Please choose the Data Source. Enter 6 for NFS, 1 for DST, 2 for DAZL:
t
>>> me = User(**attrs)
>>> me.data_source
't'
>>> me.first_name
'w'
>>> me.last_name
'q'
>>> me.rep_code
'r'
>>> me.social
'e'
>>>
答案 2 :(得分:0)
class User:
def __init__(self):
#prompt for user info and write to file
self.last_name = input("Please enter user last name:\n")
self.first_name = input("Please enter user first name:\n")
self.social = input("Please enter user social security number:\n")
self.rep_code = input("Please enter user rep code:\n")
self.data_source = input(
'''Please choose the Data Source. Enter 6 for
NFS, 1 for DST, 2 for DAZL:\n''')
```
使用此功能,user = User()
之类的呼叫将在终端发出提示。可能更有用的实现是
class User:
def __init__(
self, last_name=None, first_name=None, social=None,
rep_code=None, data_source=None):
self.last_name = last_name
self.first_name = first_name
self.social = social
self.data_source = data_source
def populate(self):
#prompt for user info and write to file
self.last_name = input("Please enter user last name:\n")
self.first_name = input("Please enter user first name:\n")
self.social = input("Please enter user social security number:\n")
self.rep_code = input("Please enter user rep code:\n")
self.data_source = input(
'''Please choose the Data Source. Enter 6 for
NFS, 1 for DST, 2 for DAZL:\n''')
这样您就可以使用User
初始化空白user = User()
,然后通过user.populate()
询问提示。
(已编辑以更改第一个__init__
示例。)