为什么我尝试打印python函数时不返回任何内容

时间:2019-12-27 08:03:22

标签: python python-3.x return

初学者,也是初学者。

我创建了一个程序,提示用户输入应试者具有的最多5种所需的必需技能。然后他们把候选人所谓的喜欢的东西放进去。愚蠢的程序,但是只是玩弄到目前为止所学到的东西。

最后一行的问题是print语句favorable(done)打印None,而不是它在函数中预先返回的内容。 favorable(done)函数返回预期的结果,但是在最后的print函数中,它返回None。不确定是什么问题。公开有关代码本身的任何一般建议。

谢谢您的帮助

k = []
user = []
count = 0


def know(count):
    while count < 5:
        count += 1
        knowledge = input("what is required for the person to know")
        k.append(knowledge)

        if knowledge == "q":
            print("your choices have been saved here are you choices")
            k.remove("q")
            print(*k, sep="\n")
            break

        if count == 5:
            print("you are out of space here are your choices ")
            print(*k, sep="\n")


def person(count):
    while count < 5:
        count += 1
        people = input("what does this person have that you liked")
        user.append(people)

        if people == "q":
            print("your choices have been saved here are you choices")
            user.remove("q")
            print(*user, sep="\n")
            break

        if count == 5:
            print("you are out of space here are your choices ")
            print(*user, sep="\n")


def favorable(done):
    if done > 3:
        return "favorable"
    if done < 3:
        return "not favorable"

    if done == 5:
        return "excellent pick"


compare = set(k).intersection(user)
done = len(compare)

print("the candidate has a total of ",
      done, "of the attributes that you listed making the candidate", favorable(done))

2 个答案:

答案 0 :(得分:0)

在函数favorable(done)中,您缺少done = 3的值的情况

因此,当您调用None时会返回favorable(3)

您应该将案例包括在前两个案例之一中,无论哪种情况都可以

if done >= 3: 
    return "favorable" 

if done <= 3: 
    return "not favorable" 

取决于您的偏好。 希望这可以帮助。 :)

答案 1 :(得分:-1)

虽然不是回答问题的一部分……一个好的经验法则,但应尽可能避免中断。您在哪里休息一下

if people == "q":
            print("your choices have been saved here are you choices")
            user.remove("q")
            print(*user, sep="\n")
            break

还有

if people == "q":
            print("your choices have been saved here are you choices")
            user.remove("q")
            print(*user, sep="\n")
            break

通常只有在绝对需要时才使用中断,并且由于无法控制的计算错误,很少需要使用户退出循环。