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

Jiten Mehta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jiten Mehta
Front End Web Development Techdegree Graduate 21,209 Points

I need help with a simple Python task...

Hello fellow coders. I need to pick your brains!!

So, I've applied for a role as front end web developer, The main languages i will utilise are HTML, CSS and javaScript. Yet the recruitment firm have given me a task on python.... Ridiculous!

The task is to find as many errors as possible in the code below.

The code attempts to loop through items in an array, and if the length of the string is above 4, then the item get appended to an array held in a variable. If less then 4, the item gets appended to a separate array.

I have never coded in python, but, i can immediately see a few errors. Here is the code:

# |% Python code will loop through list of strings. Those strings whose lengths exceed a threshold 
# % are appended to a list called long_names. The remainder are appended to a list called 
# % short_names. The number of elements in each list is then printed.
# % NB remember this is Python so indexing starts at 0.
# import numpy as np
# set up empty list to append to, and define threshold string length

long_names = ()
short_names = []
threshold = 4
# % list of strings


names = [dog , cat, mouse, horse, cow, elephant]
#  loop through list of strings, check length and append to relevant list
for i in np.arange(1,len(names),1):    
              if len(names[i]) > threshold:
                             long_list.append(names[i])
              elseif:
                             short_list.append(names[i])
print(length of long_names = , long_names)
print(length of short names = , len(short_names))

Here are some of the mistakes i have noticed. I have done a bit of research regarding the np.arrange method. However, i'm still slightly confused by it.

  1. The starting position of the arrange method is at 1. This means the first item in the names array will not be looped over.

  2. The stop position of the arrange method depends on how many characters there are in each item of the array. Therefore some items may be missed. ( im not sure about this one. )

  3. The if statement contains undefined variables. They are:

  4. long_list

  5. short_list

The needs to be changed to:

  • long_names
  • short_names
  1. The variable long_names is not an array. It has the wrong syntax. It's value should be: long_names = [];

I'm sure there is something wrong with the print method. The syntax is unfamiliar so i'm not sure. Also how is i in the loop defined?

I'm sure there is a lot more wrong with this.

Some feedback would be great. Some pointers on mistakes i have missed, or if the mistakes i have noticed are even mistakes.

Also what would be FANTASTIC is to provide a complete correction of the above code.

Hope that all makes sense guys.

Thanks so much!!

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

You came to the right place-- the community at Treehouse can lend a hand!

There are quite a few hidden syntax issues in the code. I question the need of a numpy array for such a basic computation, but numpy is fast. The excellent thing is that Treehouse workspaces are fantastic for this kind of coding. It even has numpy installed!

Myers identified the basic syntax issues:

  1. the import of numpy

  2. the long_names needs to be a list (using square bracket declaration)

In addition to that--

  1. Typography of the quotes were unicode and did not delimit the strings properly, needed to be consistent with ascii quotation marks.

  2. variables long_list and short_list needed to be long_names and short_names respectively

  3. the if elseif needed to be an if else construct

  4. the strings in the print statements needed to have the quotes in the correct location.

Below is the corrected program. Good luck with your Python learning, the language is somewhat forgiving, but is strict in a semantic sense!

# Python code will loop through list of strings. Those strings whose lengths exceed a threshold 
# are appended to a list called long_names. The remainder are appended to a list called 
# short_names. The number of elements in each list is then printed.
# NB remember this is Python so indexing starts at 0.

import numpy as np
# set up empty list to append to, and define threshold string length

long_names = []
short_names = []
threshold = 4

# list of strings
names = ['dog' , 'cat', 'mouse', 'horse', 'cow', 'elephant']

#  loop through list of strings, check length and append to relevant list
for i in np.arange(1,len(names),1):    
  if len(names[i]) > threshold:
    long_names.append(names[i])
  else:
    short_names.append(names[i])

print("length of long_names = ", len(long_names))
print("length of short names = ", len(short_names))

The comments are weird. You only need a # to start a comment but they did # % on most lines, which is odd.

The import numpy as np is probably not meant to be commented out.

long_names is a tuple (a read-only list), so you wouldn't be able to .append() to it.

I suggest installing python and numpy and see if you can get the code running.