Python运动休息命令

时间:2013-10-21 17:21:56

标签: python

我需要快速帮助学校锻炼。我必须从以下代码中删除命令中断,并使代码正常工作,而不使用其中的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)

2 个答案:

答案 0 :(得分:0)

strfind方法,搜索字符串,如果找到则返回出现索引,否则返回-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)

答案 1 :(得分:0)

试试这个:

city = 'Brooklyn'

searchable = raw_input('Insert searchable symbol: ')

print [idx for idx, ele in enumerate(city) if searchable == ele]