-1

I am trying to print every value between 100 and 999 whose integers add up to equal 7 using a for or while loop in Python

Here is what I have so far, but once I have the three digits I don't know what to do.

three_digit = range(100,999,1)
dig1 = three_digit/10
dig2 = dig1/10
dig3 = dig1%10
lucky7 = (dig1+dig2+dig3)
print '' ,lucky7,
intcreator
  • 4,206
  • 4
  • 21
  • 39
  • 1
    Where is your `for` or `while` loop? – e0k Jan 29 '16 at 04:30
  • that is where i am lost, i dont know how to make it work, im guessing its going to be while dig1+dig2+dig3 = 7: print three_digit ? – Elijah D. Jennings Jan 29 '16 at 04:32
  • 2
    Open a python terminal and try to work through this interactively. In this case, you wouldn't get past the first line without running into errors. Read up on `for` loops and `if` statements. [Dive Into Python](http://www.diveintopython.net) is a great resource. – Riaz Jan 29 '16 at 04:33
  • thank you, i will look into this – Elijah D. Jennings Jan 29 '16 at 04:37
  • Making the step (third) argument in your range explicitly set to 1 is unnecessary since 1-step is the default. – David Shaked Jan 29 '16 at 06:01

4 Answers4

1

Stack Overflow discourages coding everything for you, so here are some hints:

Break up a three digit number into single digits

Familiarize yourself with modulus (%) and floor division (//). These will help you single out the digits in a number without having to convert it to a string.

Loops

You'll probably want to use a range-based for loop, which has the syntax of for my_variable in range([enter your range]). Inside this loop you can add a condition (if statement) that prints out numbers that match your criteria.

Community
  • 1
  • 1
intcreator
  • 4,206
  • 4
  • 21
  • 39
1

If you are free the create the loop yourself, there is no need to start converting values. Just use three loops to create all possible values.

for x in range(1, 10): # hundreds
   for y in range(0, 10): # tens
       for z in range(0,10): # ones
           if (x + y + z) == 7 :
               print (x,y,z)

gives

(1, 0, 6)
(1, 1, 5)
(1, 2, 4)
(1, 3, 3)
(1, 4, 2)
(1, 5, 1)
(1, 6, 0)
(2, 0, 5)
(2, 1, 4)
(2, 2, 3)
(2, 3, 2)
(2, 4, 1)
(2, 5, 0)
(3, 0, 4)
(3, 1, 3)
(3, 2, 2)
(3, 3, 1)
(3, 4, 0)
(4, 0, 3)
(4, 1, 2)
(4, 2, 1)
(4, 3, 0)
(5, 0, 2)
(5, 1, 1)
(5, 2, 0)
(6, 0, 1)
(6, 1, 0)
(7, 0, 0)
beoliver
  • 5,579
  • 5
  • 36
  • 72
0

Your dig1 is 2 digits, so it will always be more than 7 just on its own. For example 500/10 = 50.

Take a look at this example (which is in c but should guide you).

I think a simpler way is to stringify the number and then add up the int-ified digits:

for three_digit in range(100,999,1):
    digits = str(three_digit)
    sum_of_digits = (int(digits[0]) + int(digits[1]) + int(digits[2]))
    if sum_of_digits == 7:
        print (str(three_digit) + " is lucky")
Community
  • 1
  • 1
SamN
  • 21
  • 5
0

I'm a fan of list comprehensions:

ans = [x for x in range(100,999) if sum([int(char) for char in str(x)])==7]

You can read about list comprehensions here: http://www.secnetix.de/olli/Python/list_comprehensions.hawk

David Shaked
  • 3,171
  • 3
  • 20
  • 31