Check if All Digits are Distinct
Extract the hundreds, tens, and units of a three-digit integer. Students use Python division and remainder to check every pair of digits for a repeated value.
1# Take 3-digit number2num = int(input("Enter a 3-digit number: "))34# Extract digits5digit1 = num // 1006digit2 = (num // 10) % 107digit3 = num % 1089# Check if all distinct10if 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
In this one
- Extract decimal places
- Compare all digit pairs
- Understand fixed-width assumptions
How it works
- 1
Read a three-digit integer.
- 2
Extract hundreds, tens, and units.
- 3
Require all three pairwise inequalities for distinct digits.
Edge cases
Does 121 pass?
Does 121 pass?
No. The first and last digits are equal.
Can I enter 12?
Can I enter 12?
The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.
What's going on
Distinct digits means no two digit positions contain the same value. num // 100 extracts hundreds, (num // 10) % 10 extracts tens, and num % 10 extracts units.
All three unequal comparisons must succeed. Checking only adjacent digits would miss 121, whose first and last digits repeat. The arithmetic assumes a positive three-digit input; the prompt is not a validation rule.
Does 121 pass? No. The first and last digits are equal.
Can I enter 12? The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why compare the first digit with the last?
Pick an answer to check it.
Question 2
Does 121 pass?
Pick an answer to check it.
Questions people ask
Does 121 pass?
No. The first and last digits are equal.
Can I enter 12?
The code runs but treats the hundreds place as zero, outside the stated three-digit exercise.
Why compare the first digit with the last?
Repeated digits need not be adjacent.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum