【发布时间】:2014-10-20 02:27:43
【问题描述】:
刚刚写了一个python程序来确定助记符“I before E except after C”的用处。
输入:
'I before e except when conducting an efficient heist on eight foreign neighbors. I believe my friend has left the receipt for the diet books hidden in the ceiling'
它会显示:
Number of times the rule helped: 5
Number of times the rule was broken: 5
改变了一些东西,我以为我把它们改回来了,但代码现在已经坏了,任何建议都会有所帮助
while True:
line = input("Line: ")
count = 0
h = 0
nh = 0
words = line.split()
for x in range(0, len(words)):
word = words[count]
if "ie" in word:
if "cie" in word:
nh += 1
else:
h +=1
if "ei" in word:
if "cei" in word:
h += 1
else:
nh += 1
else:
h += 0
count += 1
print("Number of times the rule helped:",h)
print("Number of times the rule was broken:",nh)
print()
【问题讨论】:
-
也许 x 应该做索引,而不是计数?
-
欢迎来到 Stackoverflow。我们不是在这里倾注您的整个程序并猜测问题可能是什么。请提供有关代码“损坏”原因的具体信息。有没有不成功的测试用例?如果是这样,请向我们提供有关它的所有详细信息。
-
抱歉没有提供足够的信息,我还是新手。发生的事情是我之前发布的输入没有任何时候规则帮助或被破坏,在返回答案的意义上没有来自程序的响应。但是,它仍然会检测较小输入中的规则破坏/遵循。我不确定为什么或如何,但我认为我已经以某种方式打破了 for 循环或逻辑
-
@musical_coder: "毛孔"。
-
不要忘记像“古代”这样的奇怪词:)
标签: if-statement for-loop python-3.x input