程序循环非常快而且无限期?不知道为什么

Program Loops Very Quickly and Indefinitely? Can't Figure Out Why

我正在 python 开发一个 Tweet Manager 程序用于我的编程 class。对于作业,我应该创建一个推文 class 来存储推文的作者、推文本身以及推文的创建时间。然后,我应该创建一个 Twitter 程序,为用户提供可供选择的选项菜单。

当我尝试 运行 我的 Twitter 程序时,它打开时没有语法错误,但一遍又一遍地打印菜单,速度非常快,没有停止。我无法弄清楚我的代码中是什么导致了这个问题。

这是我的 Twitter 代码:

    import Tweet
    import pickle
    def main():
        try:
            load_file = open('tweets.dat', 'rb')
            tweets = pickle.load('tweets.dat')
            load_file.close()
        except:
            tweet_list = []
        while (True):
            choice = display_menu()

            #Make a Tweet
            if (choice == 1):
                tweet_author = input("\nWhat is your name? ")
                tweet_text = input("What would you like to tweet? ")
                print()
            if len(tweet_text) > 140:
                print("Tweets can only be 140 characters!\n")
            else:
                print(tweet_author, ", your Tweet has been saved.")

            age = 0
            tweets = tweet.Tweet(tweet_author, tweet_text)

            tweet_list.append(tweets)

            try:
                output_file = open('tweets.dat', 'wb')
                pickle.dump(tweets, output_file)
                output_file.close()
            except:
                print("Your tweets could not be saved!")

            #View Recent Tweets
            elif (choice == 2):
                print("Recent Tweets")
                print("--------------")
                if len(tweet_list) == 0:
                    print("There are no recent tweets. \n")
                for tweets in tweet_list[-5]:
                    print(tweets.get_author(), "-", tweets.get_age())
                    print(tweets.get_text(), "\n")

            #Search Tweets
            elif (choice == 3):
                match = 0
                tweet_list.reverse()

                if tweet_list == []:
                    print("There are no tweets to search. \n")

                search = input("What would you like to search for? ")
                for tweets in tweet_list:
                    if (search in tweets.get_text()):
                        match = 1
                    if match = 1:
                        print("Search Results")
                        print("--------------")
                        print(tweets.get_author(), "-", tweets.get_age())
                        print(tweets.get_text(), "\n")
                    elif match == 0:
                        print("No tweets contained", search, "\n")

            #Quit
            elif (choice == 4):
                print("Thank you for using the Tweet Manager!")
                exit()

    def display_menu():
        print("Tweet Menu")
        print("------------")
        print()
        print("1. Make a Tweet")
        print("2. View Recent Tweets")
        print("3. Search Tweets")
        print("4. Quit")
        print()

    main()

display_menu 始终 returns None - 如果您不 return 任何东西,这就是默认值。

没问题,代码完全按照您的指示执行。

display_menu 顾名思义:显示菜单。 main 函数在循环内调用该函数。

您实际上并没有要求输入与菜单选项相对应的任何输入。

您的 display_menu 函数实际上并没有给出值。

将该函数的最后一行 (print()) 更改为

return input()

或者如果您使用的是 python 2.x

return raw_input()

显示菜单未检索到选择的任何输入...仅显示菜单。 因此,没有 if 被匹配,你无限循环。