[英]how do i get this python function to return a value?
I have a file which I'm reading in python(test.txt).我有一个正在 python(test.txt) 中读取的文件。 Sample file contents are as follows.
示例文件内容如下。
cool let's/cool
hi let's/hi
somek let's/somek
I'm searching for cool and if it exists in this file i'm trying to fetch the value let's/cool我正在寻找cool,如果它存在于这个文件中,我正在尝试获取值 let's/cool
I have tried this我试过这个
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
How do I store the value of ddda in my while loop.如何在 while 循环中存储 ddda 的值。 Is there a better way of doing things without using function?
有没有更好的做事方式而不使用函数? I'm not using any external modules.
我没有使用任何外部模块。
print(ddda) ###This is giving my value let's/cool
This is not giving you anything.这并没有给你任何东西。 It is printing .
它正在打印。 If you want to return a value, then
return
it, not print
it.如果要返回一个值,则
return
它,而不是print
它。
return ddda
Doing this will allow you to print(b)
.这样做将允许您
print(b)
。
Well, return that value:好吧,返回该值:
print(ddda)
return ddda
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.