Python 了解为什么我的循环没有重新启动

Python 了解为什么我的循环没有重新启动,python,python-2.7,loops,for-loop,while-loop,Python,Python 2.7,Loops,For Loop,While Loop,我知道为了循环回到起点,我需要把它放在一个“While”循环中。但是,如果用户输入的字符超过需要,我已经有了一个while循环来重新启动。我应该改变这种状况吗?或者,是否可以在不添加函数的情况下修复此问题 -编辑-我不清楚的错误:这是我的实际代码 abb_dict = { 'lol': 'laughing out loud', 'bfn': 'bye for now', 'cuz': 'because', 'omw': 'on my way', 'tbh'

我知道为了循环回到起点,我需要把它放在一个“While”循环中。但是,如果用户输入的字符超过需要,我已经有了一个while循环来重新启动。我应该改变这种状况吗?或者,是否可以在不添加函数的情况下修复此问题

-编辑-我不清楚的错误:这是我的实际代码

abb_dict = {
    'lol': 'laughing out loud',
    'bfn': 'bye for now',
    'cuz': 'because',
    'omw': 'on my way',
    'tbh':'to be honest',
    'afk':'away from keyboard',
    'brb':'be right back',
    'afaik':'as far as i know',
    'np':'no problem',
    'rofl':'rolling on floor laughing',
    'asap': 'as soon as possible',    
}

for k, v in abb_dict.items(): # list all the abbreviations available to be translated
    print k, v
tweet_str = raw_input('Enter the sentence with an abbreviation in it :\n').lower()

while len(tweet_str) > 160: ## if tweet_str passes 160 characters it will display a message
    print "Too long, keep it less than 160 characters"
    tweet_str= raw_input('Enter the sentence with an abbreviation in it :\n').lower()# prompts user to re-enter new string

for k in abb_dict: # looks for any key variable in dictionary
        if k in tweet_str: # if the key is seen in the user input
            print k, abb_dict[k] # prints key variable along with dictionaries value which is the definition of the abbreviation

for key, value in abb_dict.iteritems():
    tweet_str = tweet_str.replace(key, value) #replaces abbreviations with their definition
print tweet_str

while循环仅在字符串长度大于100时运行

守则:

len(str) > 100
应该是:

len(str) < 100
len(str)<100

如果您希望在字符串长度小于100时执行循环,请参见您的注释。

目前,您有一个线性程序

它收集用户输入

如果用户输入太大,它会一直请求输入,直到得到一个小的输入

然后它对输入进行一些处理

结束了


如果希望整个系统重复,则需要一个外部循环和良好的退出条件(循环何时结束)。类似于下面的psuedo代码

str = raw_input
while toLower(str) != "exit"
  while len(str) > 100
  ...
  #your other code
  ...
  str = raw_input
wend

根据您的评论,这应该让您开始:

abb_dict = {
    'lol': 'laughing out loud',
    'bfn': 'bye for now',
    'cuz': 'because',
    'omw': 'on my way',
    'tbh':'to be honest',
    'afk':'away from keyboard',
    'brb':'be right back',
    'afaik':'as far as i know',
    'np':'no problem',
    'rofl':'rolling on floor laughing',
    'asap': 'as soon as possible',    
}

user_wants_to_play = True
while user_wants_to_play:
    check = raw_input('Do you want to play? y/n:')
    if check == 'y':
        tweet_str = raw_input('Enter a statement with an abbreviation: ')
        words = tweet_str.split(' ')
        rtn_sentence = []
        for word in words:
            try:
                replacement = abb_dict[word]
                word = replacement
                rtn_sentence.append(word)
            except:
                rtn_sentence.append(word)
        print ' '.join(rtn_sentence)
    else:
        user_wants_to_play= False

这是您的实际代码还是伪代码?即时响应是
>100
表示“大于”100,而不是“小于”缩进。如果
for
循环和后面的所有内容都应该在
while
循环中,那么它不是,这是一个问题。其余代码当前无效。我真的不知道这段代码应该做什么,但我猜
for
循环应该在
while?
下。确保问题中显示了您的代码,以便我们提供帮助<代码>用于循环。。。“那是什么?”胡安,那么你必须给出一些这应该做什么的背景。for的
循环不在
while
子句的范围内,因此应该计算什么?这与我在评论中提到的
混淆的问题相同,他的评论自相矛盾。“虽然len>100”,它的评论“循环小于100”,以及打印评论“打印声明#打印消息超过100”,我赞同两个同意的声明,而不是不同意的声明。YMMV在清楚之前,不要试图回答:P我在这个问题上被更大的答案烧焦了。我注意到我的“while”语句与正常逻辑相冲突。目前正试图修复我的错误好吧,所以我是哑巴,现在我必须修复我的代码,以便它能正常工作的一切。谢谢你的参考。我还没有使用过“try”或“except”语句,因为我还是python新手,所以我还不会使用它们。我将修改我的当前程序,并尝试以您的逻辑为基础。@juan如果您使用的是字典,则您的选项是一个
try
/
,除了
get
,如果失败,它将返回
None
。基本上在这里,它在
try
中所做的是“我知道这个缩写的完整句子吗?”如果它有效,它会给出这个句子。除了
之外的
是“不,我不,保持原样”。你不能长时间避免这种结构…@juan同样,这段代码应该作为一个自包含的程序来工作。如果您将其复制/粘贴到脚本,它应该执行您希望它执行的操作。我故意把它说得冗长;粘贴一些
print
语句以查看发生了什么。