Check if All Digits are Distinct

Take a 3-digit number and check if all digits are distinct.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 1 of 10
check-if-all-digits-are-distinct.py
1# Take 3-digit number
2num = int(input("Enter a 3-digit number: "))
3
4# Extract digits
5digit1 = num // 100
6digit2 = (num // 10) % 10
7digit3 = num % 10
8
9# Check if all distinct
10if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
11 print("All digits are distinct")
12else:
13 print("Digits are not all distinct")

Output

Enter a 3-digit number: 123
All digits are distinct

Enter a 3-digit number: 122
Digits are not all distinct

What's going on

Extract digits and compare them.

Key Concepts:

Extract hundreds, tens, and units digits
Compare all pairs: digit1 != digit2, digit2 != digit3, digit1 != digit3
All must be different for distinct digits