簡體   English   中英

程序循環非常快速且無限期嗎? 無法弄清楚為什么

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

我正在使用python的Tweet Manager程序進行編程。 對於該作業,我應該創建一個Tweet類,該類存儲推文的作者,推文本身以及創建推文的時間。 然后,我應該創建一個Twitter程序,為用戶提供一個菜單選項供您選擇。

當我嘗試運行Twitter程序時,它會打開而沒有語法錯誤,但是會一遍又一遍又一遍又一遍又快速地打印出Menu,而不會停止。 我無法弄清楚代碼中是什么導致了此問題。

這是我的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始終返回None如果您不返回任何內容,則為默認值。

沒問題,代碼正好按照您的指示進行。

display_menu確實按照其名稱的含義進行操作:顯示菜單。 main函數在循環內調用該函數。

實際上,您實際上根本不會要求輸入與菜單選項相對應的任何內容。

您的display_menu函數實際上沒有提供值。

將該函數的最后一行( print() )更改為

return input()

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

return raw_input()

顯示菜單不檢索任何輸入到選擇中...僅顯示菜單。 因此,如果沒有匹配,則會無限循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM