# Code debug for newbie

**URL:** <https://discuss.python.org/t/code-debug-for-newbie/63802>\
**Category:** Python Help\
**Created:** [September 13, 2024, 12:18pm UTC](https://discuss.python.org/t/code-debug-for-newbie/63802 "2024-09-13T12:18:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aterfloris](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@Aterfloris](https://discuss.python.org/u/Aterfloris)\
**Post date:** [September 13, 2024, 12:18pm UTC](https://discuss.python.org/t/code-debug-for-newbie/63802/1 "2024-09-13T12:18:54Z")

</div>

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

What have I done wrong and why?

---

<div class="post-metadata">

**Author:** ![Kurt](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@Kurt](https://discuss.python.org/u/Kurt)\
**Post date:** [September 13, 2024, 12:29pm UTC](https://discuss.python.org/t/code-debug-for-newbie/63802/2 "2024-09-13T12:29:08Z")

</div>

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")`

---

<div class="post-metadata">

**Author:** ![vvithershins](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vvithershins/32/22484_2.png) [@vvithershins](https://discuss.python.org/u/vvithershins)\
**Post date:** [September 13, 2024, 7:31pm UTC](https://discuss.python.org/t/code-debug-for-newbie/63802/3 "2024-09-13T19:31:47Z")

</div>

> [@Aterfloris](#):
>
> prints the shorter string

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

```python
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.

> [@Aterfloris](#):
>
> `elif combined_length <= 10 and combined_length % 2 == 0:`

%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.

```python
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")

```

Sorry for the edit I misunderstood the goal.

---

<div class="post-metadata">

**Author:** ![Aterfloris](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@Aterfloris](https://discuss.python.org/u/Aterfloris)\
**Post date:** [September 14, 2024, 3:29am UTC](https://discuss.python.org/t/code-debug-for-newbie/63802/4 "2024-09-14T03:29:35Z")

</div>

Thank you for your reply. Between the two main issues I had, you help has been important for my learning. Much appreciated. Linda.

---

<div class="post-metadata">

**Author:** ![Aterfloris](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@Aterfloris](https://discuss.python.org/u/Aterfloris)\
**Post date:** [September 14, 2024, 3:37am UTC](https://discuss.python.org/t/code-debug-for-newbie/63802/5 "2024-09-14T03:37:26Z")

</div>

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.
