Sep-25-2018, 08:54 AM
I'm learning Python programming from the Mindmajix training and I just completed classes about lists, tuples, functions and methods. One of the projects following this lecture is to create a program which takes in a list as an input and outputs a corresponding string, as the following example showcases:
So I wrote the code like the below source code:
Input: ['bikes', 'cars', 'buses', 'trucks']Output: bikes, cars, buses, and trucks
So I wrote the code like the below source code:
def listToString(someList): for i in range(len(someList)): newString = str(someList[i]) if i == (len(someList)-1): print('and ' + str(someList[i])) else: print(newString, end=', ') someList = ['bikes','cars','buses','trucks'] listToString(someList)I feel like I didn't use everything (for example, some methods) to solve the problem. Is there a more efficient way to code this project?