Question:
The input function takes a 6-digit number as input. You need to break it down into separate numbers. For example: 123456 -> 1 2 3 4 5 6. This is done in order to check the equivalence of the sum of the first and second triple of numbers (the Lucky Ticket Problem). I am a beginner in programming and it is desirable to do everything with div, mod, if-else operations
a = int(input())
b = a//1000
one = b//100
two = b%11
three = b%10
c = a%1000
four = c//100
five = c%11
six = c%10
if (one+two+three)==(four+five+six):
print ('Счастливый')
else:
print ('Обычный')
Answer:
You can, of course, honestly calculate the remainders of division, etc., but the easiest way is to split the string into characters, and then translate these characters back into numbers. This is done in the second line of the following program:
a = 123456
b = map(int, str(a))
print b
if len(b)==6:
print "Happy" if b[0] + b[1] + b[2] == b[3] + b[4] + b[5] else "Unhappy"
If you really want to honestly divide by 10, then you can replace the second line of this program with the following fragment:
b = []
while a > 0:
b.append(a % 10)
a = a // 10
b = b[::-1] # так можно развернуть, если бы нам был важен порядок
print b
Now, according to the text of your program: for some reason, the remainder of division by 11 is taken (in the lines two = b%11
and five = c%11
, although it would be correct to divide by 10, and then take the remainder of division by 10 ( two = (b // 10) % 10
) You need to figure out where 11 comes from, because it's a very strange code.
Addition:
If it is important for us to support cases of input values a
less than six digits, then we can change just one line:
while len(b) < 6: # теперь в b заведомо будет 6 элементов
I also recommend studying the code from the adjacent answer (by jfs), as it has a number of interesting points.