I have a function which creates a compound string based on how many times the user wants to input the string:
def string_maker(string_list, repeat_list):
final_string = ''
for i in range(len(string_list)):
for _ in range(repeat_list[i]):
final_string += string_list[i]
return final_string
So, for example, string_maker(['a', 'b', 'c'], [1, 2, 1]) outputs abbc.
I want to make it so that if the user doesn't input a list for the repeat_list argument, it defaults to all 1s. This is how I'm trying to do it:
def string_maker(string_list, repeat_list = None):
if repeat_list == None:
repeat_list = [1] * len(string_list)
final_string = ''
for i in range(len(string_list)):
for _ in repeat_list[i]:
final_string += str
return final_string
print(string_maker(['a', 'b', 'c']))
However, I receive an error saying 'int' object is not iterable, even though the new snippet I included returns the appropriate list [1, 1, 1]. Is there a better way to go about doing this?
rangeas you did originally...stris undefined in the second code snippet (it's the name of a built-in class, so you shouldn't be using it a variable name in the first place).for _ in repeat_list[i]:...repeat_listp[i]will be anintobject, and you cannot iterate over int object, as the error clearly states.