【问题标题】:Else statement causing an infinite loop in basic word counting program?其他语句导致基本字数计数程序中的无限循环?
【发布时间】:2020-03-07 06:49:22
【问题描述】:

我正在尝试创建一个基本的单词/字符计数程序来娱乐。我试图在不检查我的笔记或任何东西的情况下做到这一点,而且我是一个相当新的自学成才的程序员。

我有一个包含 if-else 链的 while 循环。 else 语句用于检查无效输入,因为我只希望用户输入能够等于“字符”或“单词”。我希望它打印输入无效,然后返回到 while 循环的开头。

但是,每当使用 else 语句时,它都会导致无限打印循环,并且不会返回到我的 while 循环的开头。

(也感谢对我的代码的一般批评!)

我尝试过使用:继续、中断(我不希望它中断,因为我想返回到我的 while 循环的开头),以及其他一些东西,比如将我的标志设置为 False,然后再次返回 True。

import time

wordcounter_active = True

char_or_words = input("Would you like to count characters or words?" + 
" \nEnter 'characters' or 'words': " );

while wordcounter_active == True:
  #count characters or quit
  if char_or_words.lower() == ('characters'):
    count_char = input("\nEnter something to count it's characters: " + 
    "\nYou can also type 'quit' to exit the program. ");

    if count_char.lower() == ('quit'):
      print("Quitting...");
      wordcounter_active = False;
    else:
      char_input_len = len(count_char);
      print("\nThis input contains " + str(char_input_len) + " characters.");

  #count words or quit
  elif char_or_words.lower() == ('words'):
    count_word = input("\nEnter something to count it's words: " + 
    "\nYou can also type 'quit' to exit the program. ");

    if count_word.lower() == ('quit'):
      print("Quitting...");
      wordcounter_active = False;
    else:
      word_input_len = len(count_word.split())
      print("\nThis input contains " + str(word_input_len) + " words.");

  #invalid input
  else:
    print("Invalid input.");
    continue

【问题讨论】:

  • else 部分不会更改“wordcounter_active”或任何其他变量。因此,while 循环再次开始,else 再次到达并重新开始。
  • 与您的问题无关,但可能对您未来的学习有所帮助 - 请注意最后一行 (continue) 没有用。 continue 语句如果用作循环块下的最后一条命令,则始终无用。还有 - 代码缩进使用四个空格而不是两个。
  • 由于标识符wordcounter_active 不受else 块的影响,并且没有适当的输入可用于进入ifelif 块(分别为'characters''words'), while 循环别无选择,只能进入 else 块。这给了你无限循环。因此,将输入 放在 while 循环中,您的代码就可以了。

标签: python python-3.x if-statement while-loop infinite-loop


【解决方案1】:

如果我正确理解了这个问题,解决方案有两个:

第一个是:对于每个循环,程序都会要求您输入

import time

wordcounter_active = True

while wordcounter_active == True:
  char_or_words = input("Would you like to count characters or words?" + 
  " \nEnter 'characters' or 'words': " );

  #count characters or quit
  if char_or_words.lower() == ('characters'):
    count_char = input("\nEnter something to count it's characters: " + 
    "\nYou can also type 'quit' to exit the program. ");

    if count_char.lower() == ('quit'):
      print("Quitting...");
      wordcounter_active = False;
    else:
      char_input_len = len(count_char);
      print("\nThis input contains " + str(char_input_len) + " characters.");

  #count words or quit
  elif char_or_words.lower() == ('words'):
    count_word = input("\nEnter something to count it's words: " + 
    "\nYou can also type 'quit' to exit the program. ");

    if count_word.lower() == ('quit'):
      print("Quitting...");
      wordcounter_active = False;
    else:
      word_input_len = len(count_word.split())
      print("\nThis input contains " + str(word_input_len) + " words.");

  #invalid input
  else:
    print("Invalid input.");
    continue

第二个是这样:在第一个循环结束时中断代码

else:
    print("Invalid input.");
    break

【讨论】:

    【解决方案2】:

    在您的两个“else”语句中,您都没有将 wordcounter_active 变量设置为 false,这会导致无限循环。

    在设置 wordcounter_active = False 的每个 else 条件中的 print 语句之后放置一行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 2013-08-26
      • 2014-12-16
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多