2015-11-13 67 views
1

我当前的代码有效,但不是我希望它的工作方式。目前,如果我输入一个字,即“通过compc” 然后搜索字符“C” 输出将是:在Python中搜索输入字符串P2中的输入字符

'c' found at index 0 
Sorry, no occurrences of 'c' found at index 1 
Sorry, no occurrences of 'c' found at index 2 
Sorry, no occurrences of 'c' found at index 3 
'c' found at index 4 

,但我想它做的是只显示:

'c' found at index 0 
'c' found at index 4 

如果没有找到字符,那么干脆:

Sorry, no occurrences of 'c' found 

我当前的代码是:

print("This program finds all indexes of a character in a string. \n") 

inStr = input("Enter a string to search:\n") 
searchChar = input("\nWhat character to find? ") 
searchChar = searchChar[0] 

anyFound = False 
startAt = 0 


index = startAt 


while index < len(inStr): 
    if inStr[index] == searchChar: 
     anyFound = True 


    if anyFound == True: 
     print ("'" + searchChar + "' found at index", index) 
     index = index + 1 
     anyFound = False 


    else: 
     anyFound == False 
     print("Sorry, no occurrences of '" + searchChar + "' found") 
     index = index + 1 

回答

1
print("This program finds all indexes of a character in a string. \n") 

in_str = input("Enter a string to search:\n") 
search_char = input("\nWhat character to find? ") 
search_char = search_char[0] 

any_found = False 

for index, char in enumerate(in_str): 
    if char == search_char: 
     print("'%s' found at index %d" % (search_char, index)) 
     any_found = True 

if not any_found: 
    print("Sorry, no occurrences of '%s' found" % (search_char)) 
+0

感谢您的回复,虽然我试图找出我的代码有什么问题。尽管在for循环中它会更高效,但我试图在while循环内完成任务。有关于此的任何想法? – Xrin

+0

你必须在'for'后面打印'Sorry ...',而不是'for'内。 – furas

0

变化while循环结构:

anyFound = False #initialize as False 
while index < len(inStr): 
    if inStr[index] == searchChar: 
     print ("'" + searchChar + "' found at index", index) 
     anyFound = True #will change if any are found 
    #don't do anything if the char is not the same 
    index = index + 1 

#following code will only run if anyFound wasn't changed 
if not anyFound: 
    print("Sorry, no occurrences of '" + searchChar + "' found") 
0

有你的代码的一些问题,这似乎解决他们所有的Python 2:

#!/usr/bin/env python 
print("This program finds all indexes of a character in a string. \n") 
inStr = raw_input("Enter a string to search:\n") 
searchChar = raw_input("\nWhat character to find? ") 
searchChar = searchChar[0] 
anyFound = False 
startAt = 0 
index = startAt 
while index < len(inStr): 
    if inStr[index] == searchChar: 
     anyFound = True 
     print "'" + searchChar + "' found at index " + str(index) 
     index = index + 1 
    index += 1 
if not anyFound: 
    print("Sorry, no occurrences of '" + searchChar + "' found") 

的改进我我做的是:

  1. 使用raw_input而不是input让用户可以键入只是abca代替"abca"
  2. 移动的同时
+0

你不需要增加你的while循环之外的索引:) –

+0

@RNar true!复制粘贴疯狂。 –

+0

也使用括号将'print'函数作为参考,我认为OP使用的是python 3;没有'raw_input'功能。 –

0

外的最后一次检查我会向外移动的您anyFound布尔值,仅设置它一旦你发现了什么。除了我提出的索引增量外,其他所有内容都与您所做的非常相似。

anyFound = False 
while index < len(inStr): 

    if inStr[index] == searchChar: 
     print ("'" + searchChar + "' found at index", index) 
     anyFound = True 

    index = index + 1 

if not anyFound: 
    print("Sorry, no occurrences of '" + searchChar + "' found")