+ 1

Where am I going wrong. Trying to write a function that adds 2 strings index by index

def strings(str1,str2): for i in len(str1) and len(str2): str3 = (str1.index[0] +str2.index[0]/+1) print(str3) strings('Good','Bad') #for str3 to print out "GBoaodd"

11th Oct 2017, 9:46 PM
Pride Mawire
Pride Mawire - avatar
1 Odpowiedź
+ 1
First of all, and always evaluates numbers as True, so since you're always (you should) providing the operator numbers, it'll evaluate the last one, which is the last one to be returned. In your example ("Bad") it'll return 3, and it'll use it for iteration which obviously can't house enough room for "GoodBad". Anyways, if you mean to 'concatenate' strings normally, like strings("Hi",'Sup") to produce "HiSup", and aside from the fact that you can concatenate them normally using Python's tools, you really can't sum the lengths of the two strings, then use their sum range to iterate through the two strings, this doesn't make sense to me. What you should try is first adding the first string to the result, and then iterating the second one normally. I actually believe this is how normal concatenation is done.
11th Oct 2017, 10:04 PM
Muhammad Kamal
Muhammad Kamal - avatar