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 trialjdee
6,655 PointsHey guys, For this challenge, feedback is limited, so it's difficult to debug. With the below code, please tell me wha
Hey guys,
For this challenge, feedback is limited, so it's difficult to debug. With the below code, please tell me what my issue is with this code?
import os
def dir_contains(a_path, file_name_list):
# Hold return value
b = False
# Loop through files in dir
for dirname, dirnames, dirfilenames in os.walk(a_path):
# Counter
count = 0
# Loop through all dir filenames
for dirfilename in dirfilenames:
# Check if all files are in num_files
if count == len(file_name_list):
b = True;
break;
if dirfilename in file_name_list:
count+=1
continue
return b;
2 Answers
KRIS NIKOLAISEN
54,972 PointsYou could always try your code in a workspace if you want better access to debugging. I changed the order of your if blocks so that you check the value of count
after it has been incremented and removed continue
so that the check executes:
import os
def dir_contains(a_path, file_name_list):
# Hold return value
b = False
# Loop through files in dir
for dirname, dirnames, dirfilenames in os.walk(a_path):
# Counter
count = 0
# Loop through all dir filenames
for dirfilename in dirfilenames:
# Check if all files are in num_files
if dirfilename in file_name_list:
count+=1
if count == len(file_name_list):
b = True;
break;
return b;
Try a test case of 1 file in file_name_list to see why this should be so.
jdee
6,655 PointsYes, of course: workspaces! Thanks