python - Create a sentence out of a list -
python - Create a sentence out of a list -
user input 2 city name , create list my_list = ["dallas","sanjose"] next function should return:
"you visit dallas city 1 , sanjose city 2 on trip" this code:
def createsentence(my_list): sentence = "you visit " i, item in enumerate(my_list): sentence = sentence + item, "as city",i+1,"and" homecoming sentence my homecoming ('you visit dallas' , 'as city', 1 , 'and') missing sec city.
you can utilize generator look gather list of cities , numbers. utilize format create inner repeating part of string. can utilize format 1 time again add together first , lastly part respectively.
def createsentence(l): middle = ' , '.join('{} city {}'.format(city, num+1) num,city in enumerate(l)) homecoming 'you visit {} on trip'.format(middle) >>> my_list = ["dallas","sanjose"] >>> createsentence(my_list) 'you visit dallas city 1 , sanjose city 2 on trip' python list function return tuples
Comments
Post a Comment