初学者的Python-我的错误是什么?

时间:2018-02-21 02:29:15

标签: python python-3.x

这是Sheldon Cooper的友谊算法。 Python代码。 请帮我找错。 我加倍检查x200次,但似乎无法找到它。 我在读高中,所以对你们所有人来说都很容易。

print("The Friendship Algorithm \n By Dr. Sheldon Cooper Ph.D")
print("Place a phone call")
home = input("Are they home?")

if home == "yes":
print("Ask, would you like to share a meal?")
meal = input("What is their response?")

if meal == "yes":
    print("Dine together")
    print("Begin friendship!")
elif meal == "no":
    print("Ask, do you enjoy a hot beverage?")

    hot_beverage = input("What is their response?")
    if hot_beverage == "yes":
        beverage = input("Tea, coffee or cocoa?")
        if beverage == "tea":
            print("Have tea")
            print("Begin friendship!")
        elif beverage == "coffee":
            print("Have coffee")
            print("Begin friendship!")
        elif beverage == "cocoa":
            print("Have cocoa")
            print("Begin friendship!")
        else:
            print("That is not an option")

    elif hot_beverage == "no":
        while interest_cycle:
            print("Recreational activities: \n Tell me one of your interests.")
            interest = input("Do you share that interest?")
            if n > 6:
                print("Choose least objectional interest")
                interest_cycle = False
            elif interest == "no":
                print("Ask for another")
                n = n + 1
            elif interest == "yes":
                interest_cycle = False
                print("Ask, why don't we do that together?")
                print("Partake in interest")
                print("Begin friendship!")

else:
    print("That is not an option")

elif home == "no":
print("Leave message")
print("Wait for callback")

else:
print("That is not an option")

2 个答案:

答案 0 :(得分:0)

对于初学者,while循环不会执行,因为你没有设置interest_cycle的值因此它将return None而在python中,当转换为boolean时,None等同于False。 有关详细信息,请参阅此处:https://www.digitalocean.com/community/tutorials/how-to-construct-while-loops-in-python-3

并检查if home == "yes":elif home == "no": else:上的缩进。

对代码的良好工作。我喜欢可读性。

答案 1 :(得分:0)

非常好写!我做了一些修改,使它在我的电脑上运行。

首先,我将所有input()替换为raw_input,这样您就不必使用引号输入答案。

其次,我不得不修改我身边的缩进。

最后,我编辑了你的while循环,希望它看起来更像实际的流程图。

如果我是你,我还会添加一些条件,以防止程序在用户输入错误值时结束。

我希望它适合你!

print("The Friendship Algorithm \n By Dr. Sheldon Cooper Ph.D")
print("Place a phone call")
home = raw_input("Are they home?")

if home == "yes":
    print("Ask, would you like to share a meal?")
    meal = raw_input("What is their response?")

    if meal == "yes":
        print("Dine together")
        print("Begin friendship!")
    elif meal == "no":
        print("Ask, do you enjoy a hot beverage?")
        hot_beverage = raw_input("What is their response?")
        if hot_beverage == "yes":
            beverage = raw_input("Tea, coffee or cocoa?")
            if beverage == "tea":
                print("Have tea")
                print("Begin friendship!")
            elif beverage == "coffee":
                print("Have coffee")
                print("Begin friendship!")
            elif beverage == "cocoa":
                print("Have cocoa")
                print("Begin friendship!")
            else:
                print("That is not an option")

        elif hot_beverage == "no":
            n = 0
            while n<= 6:
                print("Recreational activities: \n Tell me one of your interests.")
                interest = raw_input("Do you share that interest?")

                if interest == "no":
                    print("Ask for another")
                    n = n + 1
                elif interest == "yes":
                    print("Ask, why don't we do that together?")
                    print("Partake in interest")
                    print("Begin friendship!")
                    break

                else:
                    print("That is not an option")


elif home == "no":
    print("Leave message")
    print("Wait for callback")

else:
    print("That is not an option")