【问题标题】:while loop string index out of range , python, newbiewhile循环字符串索引超出范围,python,新手
【发布时间】:2021-09-08 18:05:11
【问题描述】:

我是新手,我开始学习python, 我需要实现以下目标,如果遇到索引超出范围等错误,请停止 while 循环。

出了点问题:IndexError: string index out of range.

这是下面的代码:你们能帮帮我吗?

def mystery(st):
## Modify anything you want in this function:
i = 0
count = 0
while st[i] != '.' or len(st) >= 1:


    if st[i] in 'aeiou':
        count = count + 1
    i = i + 1
  


return count


### TESTS ###

print("********************")
print("Starting the test:")

print("********************")
print("Checking 'hello. world.'")
ans = mystery('hello. world.')
 if ans == 2:
   print("CORRECT: 'hello. world.' has 2 vowels before the first period")
 else:
    print("WRONG: 'hello. world.' has 2 vowels before the first period but the code returned",         ans)

print("********************")
print("Checking 'hello world. nice to meet you.'")
ans = mystery('hello world. nice to meet you.')
 if ans == 3:
    print("CORRECT: 'hello world. nice to meet you.' has 3 vowels before the first period")
 else:
   print("WRONG: 'hello world. nice to meet you.' has 3 vowels before the first period but the        code returned", ans)

 print("********************")
 print("Checking ' '")
ans = mystery(' ')
 if ans == 0:
   print("CORRECT: The string ' ' has no vowels")
else:
   print("WRONG: The string ' ' has no vowels but the code returned", ans)

print("********************")
print("Checking 'dddda'")
ans = mystery('dddda')
  if ans == 1:
print("CORRECT: 'dddda' has 1 vowel")
 else:
   print("WRONG: 'dddda' has 1 vowel but the code returned", ans)

print("********************")    
print("Tests concluded, add more tests of your own below!")
print("********************")

【问题讨论】:

  • 您的 while 条件中有 st[i],它在循环中的 i = i + 1 之后运行。这意味着您没有采取任何措施来防止 i 超出范围。您必须设计循环,使其永远不会尝试访问列表末尾之外的任何值。
  • while st[i] != '.' or len(st) >= 1: 可以“永远”运行(直到出现错误)如果 '.'不在st 中且st 不为空。
  • while i < len(st) and st[i] != '.':
  • 嗨,尝试了以下方法:'while st[i] !='.'和 k != " " 和 i

标签: python loops while-loop


【解决方案1】:

如果你想在不终止程序的情况下停止循环(即到达“返回”),那么只需将这个“while”包装到 try: ... 除了块

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
  • 我不能使用 except 块...这确实是解决方案...:>
【解决方案2】:

我成功回答了我自己的问题!!!是的!!! 这是代码:

    i = 0
count = 0
k = st
z = len(st)
while st[i] != '.' and k != " ":
    
    if st[i] in 'aeiou':
        count = count + 1
    i = i + 1
    if i >= z:
        break

return count

### TESTS ###

print("********************")
print("Starting the test:")

print("********************")
print("Checking 'hello. world.'")
ans = mystery('hello. world.')
   if ans == 2:
      print("CORRECT: 'hello. world.' has 2 vowels before the first period")
   else:
      print("WRONG: 'hello. world.' has 2 vowels before the first period but the  code returned", ans)

print("********************")
print("Checking 'hello world. nice to meet you.'")
ans = mystery('hello world. nice to meet you.')
if ans == 3:
    print("CORRECT: 'hello world. nice to meet you.' has 3 vowels before the first  period")
else:
    print("WRONG: 'hello world. nice to meet you.' has 3 vowels before the first   period but the code returned", ans)

   print("********************")
   print("Checking ' '")
   ans = mystery(' ')
   if ans == 0:
       print("CORRECT: The string ' ' has no vowels")
   else:
       print("WRONG: The string ' ' has no vowels but the code returned", ans)

   print("********************")
   print("Checking 'dddda'")
   ans = mystery('dddda')
   if ans == 1:
       print("CORRECT: 'dddda' has 1 vowel")
   else:
      print("WRONG: 'dddda' has 1 vowel but the code returned", ans)

   print("********************")    
   print("Tests concluded, add more tests of your own below!")
   print("********************")

''' '''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2017-04-27
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    相关资源
    最近更新 更多