0

This is what I want to accomplish:

  1. let the user type in a number with four digits
  2. if this number is not equal to 6174 (Kaprekar’s constant) then sort the numbers in two ways: a. from the biggest to the smallest number b. from the smallest to the biggest
  3. Subtract the bigger number with the smaller
  4. If the result is not equal to 6174, then do the calculation again and write the result for each and every calculation
  5. When the result is equal to 6174, write a message to show that the calculation is done

This is what I’ve tried:

print("type in a number")
number = (input())
while number != 6174:
    start_big = "".join(sorted(number, reverse=True))
    start_small = "".join(sorted(number))
    number = (int(start_big)-int(start_small))
    print(number)
print("Calculation finnished!")

I’m getting the error:

start_big = "".join(sorted(number, reverse=True)) TypeError: 'int'
object is not iterable
Cœur
  • 37,241
  • 25
  • 195
  • 267
Peter Nydahl
  • 119
  • 1
  • 9

4 Answers4

1

When you calculate this:

number = (int(start_big)-int(start_small))

The type of number becomes int, so in the next iteration the error occurs.

One solution would be

print("type in a number")
number = input()
while number != "6174":
    start_big = "".join(sorted(number, reverse=True))
    start_small = "".join(sorted(number))
    number = str((int(start_big) - int(start_small)))
    print(number)
print("Calculation finnished!")
Santos
  • 194
  • 2
  • 4
0

You are almost there. You need to convert number to an iterable. I suggest:

    start_big = "".join(sorted(str(number), reverse=True))  
    start_small = "".join(sorted(str(number)number))  
0

you have to convert the input number to an iterable, you can just do

number = iter(number)

also you need to change the while loop condition:

while int(number) != 6174:

for printing the number just do this:

number = str((int(start_big)-int(start_small)))
bmbigbang
  • 1,318
  • 1
  • 10
  • 15
0

You have multiple issues with your solution:

  1. input() always returns a string, so number will never be an integer and as such it will never be equal to 6174. So your loop will still run for an input of 6174.

    You need to convert the string into a number first, you can do that by simply calling int() on the string. Note that this may fail, so you might want to ask the user for input until they enter a valid number.

  2. Once number is an integer, it is no longer an iterable, so you cannot call sorted() on it. If you want to sort the digits, then you need to convert it into a string first: sorted(str(number)).

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
  • Point 1: Once through the loop, `number` is an int, so the loop will only run once. – Martin Bonner supports Monica Feb 14 '16 at 16:57
  • Why do I have to change the integers to strings when I'm sorting the numbers? I thought strings where only text but now I´m changing integers to strings to be able to use them in a calculation which means that the strings acts like numbers. I also wonder why integers can´t be used in the while-loop. Why is an integer not a iterable? – Peter Nydahl Feb 14 '16 at 18:35
  • 1
    How do you *sort* a number like `6174`? It’s just one number, so there is nothing you can sort. What you want to do is sort the *digits* of the number. In order to get the digits of the number, you can convert it to a string `'6174'` and then you can iterate over every character of that string (`'6'`, `'1'`, `'7'`, `'4'`). An integer can be used in the condition of a while loop but your initial value of `number` is not an integer but a string (since it comes from `input()`). – poke Feb 14 '16 at 18:39
  • I get it! Thanks a lot! :) – Peter Nydahl Feb 14 '16 at 18:45