如何设置我的 if 语句变量与我的函数相同

How do I set it that my if statements variable is the same as my function

对于问题的措辞,我深表歉意,因为我不太确定该如何表达。 我一直试图做的是让用户输入他们的输入,然后当我的 if 语句被执行并调用他们的输入时,我可以询问 if 语句以查看他们的输入是否等于另一个变量由函数设置。 我不确定这是否有意义,但这是我的代码,我会尝试解释更多。

def randomint(a,b):
    return random.randint(a,b)
def random_line(fname, num):
    f1 = open(fname ,"r")
    lines = f1.readlines()
    return lines[num]
def random2(fname, num):
    f2 = open(fname ,"r")
    line1 = f2.readlines()
    return line1[num]
def f1():
    print(random_line('songs.txt' , nu))
def f2():
    random_line('songnames.txt' , nu)
nu = randomint(0,4)

f1()
f2()
a2 = input("what is the songs name\n")
if a2 == f2():
    print("yes")
else:
    print("no")

如您所见,我上面的函数调用由参数的文件名设置的外部文件,然后是分配的行的随机数。我想要做的是设置它,以便当我使用 if 语句时它会检查

if a2 == f2():

如果等于同一个东西。如果他们输入了正确的答案,我希望它输出 yes 以确保它正常工作。我的问题是,当我 运行 代码并尝试获取 "yes" 时,尽管知道答案,但我得到的输出为否。 当我这样做时,我会展示它是如何 运行 的。

Post Malone C

yes
Circles
no

这只是一个例子,但事实就是如此。想知道如何确保它输出是,抱歉,如果我措辞糟糕,不完全确定如何输入它。

您的 f1()f2() 中没有 return。应该是:

def f2():
    return random_line('songnames.txt' , nu)