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
Casey Huckel
Courses Plus Student 4,257 PointsCan anyone create a code for this?
In this assignment we will be reading and writing to files and creating a library that you will import for use in your main program.
Givens: I have created a text file, Rumbers.txt, that contains 5000 random numbers. These numbers are stored in ten columns. The columns are separated by tabs and rows end with a newline.
You already have your isHarshad() function from assignment 1:
def isHarshad(num):
num_string = str(num)
num_list = list(num_string)
num_int_list = []
num_sum = 0
for x in num_list:
num_sum += int(x)
num_result = num/num_sum
if num_result == round(num_result):
return True
else:
return False
harshad_numbers = []
for num in range(1, 501):
if isHarshad(num) == True:
harshad_numbers.append(num)
print(harshad_numbers)
We are going to use it here.
The Task: You need to find/create three things
The sum of the Harshad numbers in ‘Rumbers.txt’. An output file containing all the Harshad numbers with a ‘7’ in the tens place. The numbers that are Harshad, have a 7 in the second column and are evenly divisible by 41.
The Hows or “What we need to see”
You need to provide three files.
A file called “myLib.py” Inside this file should reside two functions, isHarshad() and Siete(). isHarshad() will accept a string and return True if the input is a Harshad number and False if it is not. isSiete() will accept a string and will return True if the second column digit of the number is a ‘7’ and False if it is not. Lastly, you are to create a constant variable called “Seaver” and set it equal to 41. All three of these items will be imported by your main program. A main program that reads in “Rumbers.txt” and creates the three deliverables above. This main program will open Rumbers.txt, import functions and the Seaver variable from myLib.py, do the processing necessary to find the solutions and create the output file we need, and then will close “Rumbers.txt” and the output file (see below). As output this program will print out the sum of the Harshad numbers and print the numbers that are harshad numbers with a seven in the tens column and are also multiples of the Seaver constant. An output file named “HarshOut.txt” that contains all of the Harshad numbers that have a ‘7’ in their second column.
Chris Freeman
Treehouse Moderator 68,468 PointsThis is clearly a homework assignment for another course. The purpose of this Community is help answer questions and provide clarity for students stuck on a problem. We are not here to do your homework.
We would love to help you with some aspect of this problem. Please post your code and a specific question relating to your code that you need help with in order to solve the problem.
6 Answers
Casey Huckel
Courses Plus Student 4,257 PointsOk Chris, you are right. I am trying to get a lot of work done right now and was hoping to cut corners, but it's better that I don't. Here's my first question:
How do I import numbers from a dataset and print out a certain group of these numbers (called Harshad numbers)?
Thanks
Casey Huckel
Courses Plus Student 4,257 PointsOk I figured that out:
import string
def isHarshad(num):
num_string = str(num)
num_list = list(num_string)
num_int_list = []
num_sum = 0
for x in num_list:
num_sum += int(x)
num_result = num/num_sum
if num_result == round(num_result):
return True
else:
return False
harshad_numbers = []
for num in range(1, 501):
if isHarshad(num) == True:
harshad_numbers.append(num)
#print(harshad_numbers)
# main program
# Open file, read data, close file
infile = "Rumbers.txt"
f = open(infile,'r')
data = f.read()
f.close()
# prepare out data
data = data.replace('\n','\t')
data = data.split('\t')
# iterate through the list and find the Harshads
accum = 0
num_has_seven = []
for i in data:
if isHarshad(int(i)) == True:
#print(int(i))
accum = accum + int(i)
if (len(str(i))>1 and str(i)[-2] == 7):
num_has_seven.append(str(i))
print("The sum is", accum)
Now my next question is how do you locate and print every 7 in the tens place of these numbers
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,468 PointsHard to tell if i (from data), is a string. if i is already a string based on for i in data, then you don't have to recast it using str(i).
The ten's digit is always second from the right and can be indexed by [-2] as is done in the above code. A short cut for being sure you don't get an index error is to prepend a "0":
if ('0' + i)[-2] == '7':
# do something
But being Python errors are ok:
try:
if i[-2] == '7':
# do something
num_has_seven.append(i)
except IndexError:
pass
Edit: Fixed typo.
Ariel Saulog
30 PointsThanks... I’ve been having problems with code as well...
Casey Huckel
Courses Plus Student 4,257 Pointsif i[-2] == '7':
# do something
num_has_serven.append(i)
except IndexError:
pass
Led to "invalid syntax"
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,468 PointsDid you leave off the try?
In [50]: i = "73"
In [51]: num_has_seven = []
In [52]: try:
if i[-2] == "7":
num_has_seven.append(i)
except IndexError:
pass
....:
In [53]: num_has_seven
Out[53]: ['73']
Ariel Saulog
30 PointsWhy would you need to use try? I’m curious to understand this method
Casey Huckel
Courses Plus Student 4,257 PointsI put the "try" in this time:
try:
if i[-2] == "7":
num_has_seven.append(i)
except IndexError:
pass
I got "expected an indented block"
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,468 PointsIf is not indented into try code block.
Casey Huckel
Courses Plus Student 4,257 PointsOk I'm working on it and now I get nothing and it must be because you left out print?
Casey Huckel
Courses Plus Student 4,257 PointsOk I've got this:
print(i[-2])
Which leaves 9 in the output but I need a list of the numbers
jason chan
31,009 Pointsjason chan
31,009 PointsI formatted your question for easier readability.