【问题标题】:How do I set it that my if statements variable is the same as my function如何设置我的 if 语句变量与我的函数相同
【发布时间】:2019-12-06 17:59:39
【问题描述】:

对问题的措辞表示歉意,因为我不完全确定如何表达。 我一直在尝试做的是让用户输入他们的输入,然后当我的 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():

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

Post Malone C

yes
Circles
no

这只是一个例子,但这就是发生的事情。想知道如何确保它输出是,对不起,如果我措辞很糟糕,不完全确定如何输入。

【问题讨论】:

  • f2() 总是返回None

标签: python python-3.x function variables selection


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 2016-02-16
    • 2021-09-14
    • 2022-08-19
    • 2018-06-27
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多