Python code count letters for loop -
write function countletter() should have 'for' loop steps through list below , prints name of city , number of letters in city's name. may use len() function.
citylist = ["kentucky","new york","la", "toronto", "boston","district of columbia"]
i using python 3.5 in spyder. having trouble extracting letters list , having them print out within loop.
what have:
def countletter(citylist): city = len(citylist) ct = 0 in citylist: city = (ne[i])
and stuck. fear may entirely wrong. struggling on how print this.
the output should be:
kentucky has 8 letters.
new york has 12 letters.
la has 2 letters.
toronto has 7 letters.
boston has 6 letters.
district of columbia has 20 letters.
thank help!
you don't need use indices. iterate citylist
; for
loop yield each city.
def countletter(citylist): city in citylist: n = len(city) print(city, 'has', n, 'letters.') citylist = ["kentucky","new york","la", "toronto", "boston","district of columbia"] countletter(citylist)
output:
kentucky has 8 letters. new york has 8 letters. la has 2 letters. toronto has 7 letters. boston has 6 letters. district of columbia has 20 letters.
Comments
Post a Comment