0

我需要快速帮助学校锻炼。我必须从以下代码中删除命令 break 并在没有该 break 命令的情况下使代码正常工作。我将非常感谢您的建议。

city = 'Brooklyn'
searchable = input('Insert searchable symbol: ')
for index,symbol in enumerate(city):    # for every index
    if symbol == searchable:            # control if examining is found
        print("Symbol has been found at index: ", index) # if yes, print index
        break # stop search
    else:
        print('Symbol',searchable,'is not represented in word',city)
4

2 回答 2

0

尝试这个:

city = 'Brooklyn'

searchable = raw_input('Insert searchable symbol: ')

print [idx for idx, ele in enumerate(city) if searchable == ele]
于 2013-10-21T18:02:08.463 回答
0

strhasfind方法,它搜索字符串,如果找到则返回出现索引,如果没有则返回 -1:

city = 'Brooklyn'
searchable = input('Insert searchable symbol: ')
index = city.find(searchable)
if index >= 0:
    print("Symbol has been found at index: ", index) # if yes, print index
else:
    print('Symbol',searchable,'is not represented in word',city)
于 2013-10-21T17:25:45.347 回答