﻿ Python: Reads n digits chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number(s) - w3resource # Python: Reads n digits chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number(s)

## Python Basic - 1: Exercise-48 with Solution

Write a Python program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.

Input:
Two integers as number of combinations and their sum by a single space in a line. Input 0 0 to exit.

Sample Solution:

Python Code:

``````import itertools
print("Input number of combinations and sum, input 0 0 to exit:")
while True:
x, y = map(int, input(). split())
if x == 0 and y == 0:
break
s = list(itertools.combinations(range(10), x))
ctr = 0
for i in s:
if sum(i) == y:
ctr += 1

print(ctr)
``````

Sample Output:

```Input number of combinations and sum, input 0 0 to exit:
5 6
2 4
0 0
2
```

Flowchart: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Checks if the given number falls within the given range.

Example:

```def tips_range(n, start, end = 0):
return start <= n <= end if end >= start else end <= n <= start
print(tips_range(2, 4, 6))
print(tips_range(4, 8))
print(tips_range(1, 3, 5))
print(tips_range(1, 3))
```

Output:

```False
True
False
True
```