As a newcomer to programming, ~ 8 weeks, I have a question about why my code keeps printing the same concatenated output regardless of the code options. My attempt at the function is below including the doc string of what I am trying to achieve.
doc
a function get_them that takes two strings and prints the shorter string if their combined length is greater than 10 and is an even number. If the combined length is less or equal 10 and is an odd number, it should print the longer string. Otherwise, it should print both strings concatenated.
‘’’
def get_them(string1, string2):
combined_length = len(string1) + len(string2)
if combined_length > 10 and combined_length % 2 == 0:
if len(string1) < len(string2):
print(string1)
elif combined_length <= 10 and combined_length % 2 == 0:
if len(string1) > len(string2):
print(string2)
else:
print(string1 + ' + ' + string2)
‘’’
calling the function
get_them(‘get it’,‘now’)
it prints the output get it + now instead of now
Both checks are for even.
Try the second changed to “…combined_length % 2 == 1:”
But there seams to be another logical error:
A function get_them that takes two strings and prints the shorter string if their combined length is greater than 10 and is an even number. If the combined length is less or equal 10 and is an odd number, it should print the longer string.
You should check also the call get_them("now", "get it")
You’re only printing the shorter string if it is string1, but you’re supposed to print the shorter one, no matter if it is string1 or string2.
It’s also possible that they could be the same length.
It should be
if combined_length > 10 and combined_length % 2 == 0:
if len(string1) < len(string2):
print(string1)
elif len(string1) > len(string2):
print(string2)
else: # if they are equal in length
print("string1 and string2 are equal in length")
Then for when the combined_length is less than or equal to ten and is an odd number, you also only print string2 if it has fewer characters than string1, but you’re supposed to print whichever one has more, not fewer, whether it is string1 or string2 doesn’t matter.
You’re supposed to do this when the combined_length is odd but you do it if it’s even instead.
%2 == 0 means in this case that if the number is divisible by 2, then it is even.
%2 != 0 means if the number is not divisible by 2, (it has a remainder) then it is odd.
def get_them(string1, string2):
combined_length = len(string1) + len(string2)
print("combined length : ",combined_length)
if combined_length > 10 and combined_length % 2 == 0:
if len(string1) < len(string2):
print(string1)
elif len(string1) > len(string2):
print(string2)
else:
print("string1 and string2 are equal in length :",len(string1))
elif combined_length <=10 and combined_length %2 != 0:
if len(string1) > len(string2):
print(string1)
elif len(string1) < len(string2):
print(string2)
else:
print("string1 and string2 are equal in length :", len(string1))
else:
print(string1 + ' + ' + string2)
get_them("get it", "now")
I really appreciate the time you took to look at my request and provide the indepth feedback you did. I understand what I did wrong and have corrected my code. I was told the test function calls would not have even strings so I would not need to provide for that condition. Having both string1 and string2 options needed to be included to ‘fix’ my code.
Thanks, Linda.