I'm working on a Tweet Manager program in python for my programming class. For the assignment, I'm supposed to create a Tweet class that stores the author of a tweet, the tweet itself, and the time the tweet was created. Then, I'm supposed to create a Twitter program that gives users a menu of options to choose from.
我正在python中为我的编程类开发一个Tweet Manager程序。对于作业,我应该创建一个Tweet类来存储推文的作者,推文本身以及创建推文的时间。然后,我应该创建一个Twitter程序,为用户提供一个选项菜单供选择。
When I try to run my Twitter program, it opens without syntax errors, but prints the Menu over and over and over again really rapidly without stopping. I can't figure out what in my code is causing this problem.
当我尝试运行我的Twitter程序时,它会打开而不会出现语法错误,但会在不停止的情况下一遍又一遍地打印菜单。我无法弄清楚我的代码中是什么导致了这个问题。
Here is my Twitter code:
这是我的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()
4 个解决方案
#1
There's no problem, the code is doing precisely what you told it to.
没有问题,代码正是按照你告诉它做的。
display_menu
does exactly what the name implies: displays the menu. The main
function calls that function inside a loop.
display_menu正如名称所暗示的那样:显示菜单。 main函数在循环内调用该函数。
At no point do you actually ask for any input corresponding to the menu options.
在任何时候你都不会要求输入与菜单选项相对应的任何输入。
#2
display_menu
always returns None
- that's the default if you don't return anything.
display_menu总是返回None - 如果你没有返回任何东西,这是默认值。
#3
Display menu does not retrieve any input into choice... Just displays the menu. Thus, no if is matched and you loop indefinitely.
显示菜单不会检索任何输入选择...只显示菜单。因此,如果匹配则无关,并且无限循环。
#4
Your display_menu
function doesn't actually give a value.
您的display_menu函数实际上没有给出值。
Change the last line of that function (print()
) to
将该函数的最后一行(print())更改为
return input()
or if you are using python 2.x
或者如果您使用的是python 2.x.
return raw_input()
#1
There's no problem, the code is doing precisely what you told it to.
没有问题,代码正是按照你告诉它做的。
display_menu
does exactly what the name implies: displays the menu. The main
function calls that function inside a loop.
display_menu正如名称所暗示的那样:显示菜单。 main函数在循环内调用该函数。
At no point do you actually ask for any input corresponding to the menu options.
在任何时候你都不会要求输入与菜单选项相对应的任何输入。
#2
display_menu
always returns None
- that's the default if you don't return anything.
display_menu总是返回None - 如果你没有返回任何东西,这是默认值。
#3
Display menu does not retrieve any input into choice... Just displays the menu. Thus, no if is matched and you loop indefinitely.
显示菜单不会检索任何输入选择...只显示菜单。因此,如果匹配则无关,并且无限循环。
#4
Your display_menu
function doesn't actually give a value.
您的display_menu函数实际上没有给出值。
Change the last line of that function (print()
) to
将该函数的最后一行(print())更改为
return input()
or if you are using python 2.x
或者如果您使用的是python 2.x.
return raw_input()