Reputation: 1383
I have following code below.
class NameParser:
def __init__(self):
self.getName
def getName(self, name):
splitName = name.split(' ')
surname = splitName.pop()
for i in range(len(splitName)):
print('Name: %s' % splitName[i])
return('Surname: %s' % surname)
np = NameParser()
print(np.getName("ali opcode goren"))
# output: name: ali, name: opcode, surname: goren
How do i return two values? Like following code:
for i in range(len(splitName)):
return('Name: %s' % splitName[i])
return('Surname: %s' % surname)
# output: name ali: (error) i want all values name, name, surname
I want all values but just one output. How can I solve this problem?
Upvotes: 0
Views: 610
Reputation: 281
or you can try
class test():
map = {}
for i in range(10):
map[f'{i}'] = i
return map
Upvotes: 0
Reputation: 10213
pop()
method and assign it to surname
variable.IndexError
exception.for loop
and assign the value to user_name
variable.surname
in string again.Demo:
class NameParser:
def __init__(self):
pass
def getName(self, name):
#- Spit name and again check for empty strings.
splitName = [i.strip() for i in name.split(' ') if i.strip()]
#- Get Surname.
try:
surname = splitName.pop()
except IndexError:
print "Exception Name for processing in empty."
return ""
user_name = ""
for i in splitName:
user_name = "%s Name: %s,"%(user_name, i)
user_name = user_name.strip()
user_name = "%s Surname: %s"%(user_name, surname)
return user_name
np = NameParser()
user_name = np.getName("ali opcode goren abc")
print "user_name:", user_name
Output:
user_name: Name: ali, Name: opcode, Name: goren, Surname: abc
Upvotes: 3
Reputation: 1170
Try this:
class NameParser:
def __init__(self):
self.getName
def getName(self, name):
listy = [] # where the needed output is put in
splitName = name.split(' ')
for i in range(len(splitName)):
if i==(len(splitName)-1):#when the last word is reach
listy.append('Surname: '+ splitName[i])
else:
listy.append('Name: '+ splitName[i])
return listy
nr = NameParser()
print(nr.getName("ali opcode goren"))
# output: name: ali, name: opcode, surname: goren
whithout loop:
class NameParser:
def __init__(self):
self.getName
def getName(self, name):
listy = [] # where the needed output is put in
splitName = name.split(" ")
listy ="Name",splitName[0],"Name",splitName[1],"Surname",splitName[2]
return listy
nr = NameParser()
print(nr.getName("ali opcode goren"))
# output: name: ali, name: opcode, surname: goren
Upvotes: 1
Reputation: 4493
you can just do this:
def getName(self, name):
return name.split(' ')
It will return a tuple
def get_name(name):
return name.split(' ')
>>> get_name("First Middle Last")
['First', 'Middle', 'Last']
Upvotes: 1
Reputation: 5600
Try to use yield
class NameParser:
def __init__(self):
self.getName
def getName(self, name):
splitName = name.split(' ')
surname = splitName.pop()
for i in range(len(splitName)):
yield ('Name: %s' % splitName[i])
yield ('Surname: %s' % surname)
np = NameParser()
for i in (np.getName("ali opcode goren")):
print i
Upvotes: 1