![](/img/trans.png)
[英]How do I get a function that is called within another function to return a value? PYTHON
[英]how do i get this python function to return a value?
我有一个正在 python(test.txt) 中读取的文件。 示例文件内容如下。
cool let's/cool
hi let's/hi
somek let's/somek
我正在寻找cool,如果它存在于这个文件中,我正在尝试获取值 let's/cool
我试过这个
file = open('test.txt')
my_value='cool'
def test_function(my_value):
for line in file:
#fields = line.strip().split()
if line.find(my_value) != -1:
ddda=line.rsplit(" ",1)[1]
print(ddda) ###This is giving my value let's/cool
while true:
b=test_function(my_value)
print(b) ##not able to fetch the value of ddda here
如何在 while 循环中存储 ddda 的值。 有没有更好的做事方式而不使用函数? 我没有使用任何外部模块。
print(ddda) ###This is giving my value let's/cool
这并没有给你任何东西。 它正在打印。 如果要返回一个值,则return
它,而不是print
它。
return ddda
这样做将允许您print(b)
。
好吧,返回该值:
print(ddda)
return ddda
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.