I got the task to alternately combine the letters of two strings with the same length.
For example:
Inputstring 1: \"acegi\"
Inputstring 2: \"bdfhj\
One of the first things you should learn when programming is to use meaningful variable names, not cryptic, 1-letter names.
Your code is not alternating between the two input strings. You're looping through the first string, then looping through the second string, and never repeating.
I'm not sure what the point of the c
variable is. You set it to 1
at the beginning of the script, then add 1 to it later, but then the script ends. Was there supposed to be another loop around all that code?
The loop that checks if f
is in range(c, c+1)
could just be f = c
, there's no point to looping.
The error is coming from
x = x + f
because x
is a string and f
is an int
. I suspect you wanted to do x = x + s[f]
.
The whole thing can be simplified greatly.
string1 = input("Enter string 1: ")
len1 = len(string1)
string2 = input("enter string 2: ")
len2 = len(string2)
if len1 != len2:
print("Inputs must be the same length")
else:
result = ""
for i in range(len1):
result += string1[i]
result += string2[i]
print(result)