Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Sillycase challenge. I get the error "where's sillycase()"

I get the error "Where's sillycase()" when I've clearly defined sillycase.

This is my code.

def sillycase(a_list):
  return a_list[:int(len(a)/2)].lower() + a_list[int(len(a)/2*-1):].upper()

I've tested my code in the python IDE

>>> a
'somestring'
>>> a[:int(len(a)/2)].lower() + a[int(len(a)/2*-1):].upper()
'somesTRING'
>>> 

So what could I be doing wrong?

I've also tested out the function.

>>> def sillycase(a):
...   return a[:int(len(a)/2)].lower() + a[int(len(a)/2*-1):].upper()

>>> somecase = 'somestring'
>>> sillycase(somecase)
'somesTRING'

2 Answers

I took your code an changed it so that it worked. Look at the changed code first then to see if you can spot the changes, then look below for my change list.

def sillycase(a_list):
  return a_list[:int(len(a_list)/2)+1].lower() + a_list[int(len(a_list)/2)+1:].upper()

1) changed len(a) to len(a_list) <-- There is no "a" variable in your code to find the length of.

2) added a +1 to the first slice int since this the problem requires you to capture thr first 1/2 of a 7 letter string and wants the first half to round up.

3) removed "*" from the second slice int since this does nothing to my knowledge

4) added +1 to the second slice int to make sure it starts where the first slice left off.

If you are looking to make this code more flexible, so that it returns the correct results for both odd and even lengths strings you could do something like this.

def sillycase(a_list):
    remain = len(a_list) % 2
    return a_list[:int(len(a_list)/2) + remain].lower() + a_list[int(len(a)/2) + remain:].upper()

In this upgraded version of the program the remain variable captures any remainder resulting from dividing the length of string a_list by 2. This remainder is then added to the first slice and the second slice of the word. If len(a_list) is an even number the remainder will be zero so the slices will be evenly split in half. In the case that len(a_list) is an odd number though the remainder will be added to the slice ints thus causing the first half to be rounded up.

Hope that helps and keep up the great work.

David Katz-Wigmore

It seems that my issue was the rounding up or down on the given argument. However the question never stated that it was an odd number of characters given to the argument. Would have been helpful if I knew that was the case then I could have used round. Regardless, I've already used a similar code already and solved the issue. I just wanted to know why my first code didn't work, when using it in the IDE (different variable name, to be honest, I was aware of that).

The question states "Create a function named sillycase that takes a string and returns that string with the first half lowercased and the last half uppercased." Nothing about an odd number of characters stated, I guess that's a "gotcha of programming/bug fix"

I agree that at times the challenges' text is too general and a bit more specificity would really help people out.

In this case though I think the idea was to force students to think through all the possible strings that could be entered and write code that could accommodate those variations. This is a big part of the work of programming.

Once again though, perhaps they should say that somewhere so that students know to start thinking about problems that way.