I am learning python, so I wrote this function, but I see I can’t print the values. How can I retrieve those values outside the function?
txt = "KİMLİK BİLGİLERİ Kimlik No 511345216748 Adı Soyadı : : Marco : Polo Adres Tipi Adres Türü Adres No Adres Yerleşim Yeri Adresi Yurtiçi 28693222128232166 CEREALİ MAH. 17 SK. GATTİ APT. BLOK NO: 24 İÇ KAPI NO: 18 PIOLTELLO / MILANO AÇIKLAMALAR"
def f(text, idcard, name, surname, addressN, addressLong):
list_word = txt.split()
for i in range(len(list_word)):
start = txt.find("Adresi") + len("Adresi")
end = txt.find("AÇIKLAMALAR")
sub = txt[start:end]
addressLong = ' '.join(sub.split(' ')[3:])
return idcard, name, surname, addressN, addressLong
name = ""
surname = ""
addressN = ""
addressLong = ""
f(txt, idCard, name, surname, addressN, addressLong)
print("id",idCard,"name", name,"surname", surname,"addressN", addressN,"long", addressLong)
You need to assign the value return from a function to a variable. Since a function call has a value, you can print the returned value directly also.
def add(a, b) :
return a + b
c = add(10, 30)
Arguments are for passing values to a function. You cannot return a result through parameter in Python.
If you want to return more than one values, then you can do this, for example
def sumprod(a, b):
return a+b, a*b
s, p = sumprod(5, 8)
you need to bind it to a variable if you want the values to persist. in this example, I’ve assigned the return to the variable x
Thank you for your answer!
Thank you for your answer and time!
You’re really passing just one string to the function, so it should be more like
return a, b, c, d, e //with a, b, c, d, e named appropriately
result = f(txt)
a, b, c, d, e = f(txt) //with a, b, c, d, e renamed appropriately
If you have just two or three return values, you might name them individually instead of using indexing. (You can also take an OOP approach and return an object.)
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.