π Python Crash Course – Chapter 7: User Input And while loops – Exercise 7-3 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 7-3
Task:
Ask the user for a number, and then report whether the
number is a multiple of 10 or not
✅ Solution Code:
number = int(input("Enter a number: "))
# Check if the number is a multiple of 10
if number % 10 == 0:
print(f"{number} is a multiple of 10.")
else:
print(f"{number} is not a multiple of 10.")
π§ Code Explanation:
-
Convert user input to an integer:
The user's input is converted to an integer using the
int()
function and stored in the variablenumber
. This ensures that the input can be used for mathematical operations. -
Check for multiples of 10:
An
if
statement checks whether the number is a multiple of 10 by using the modulus operator (%
). The conditionnumber % 10 == 0
evaluates toTrue
if the remainder when dividing the number by 10 is 0, indicating that the number is a multiple of 10. -
Display appropriate messages:
- If the condition is
True
, a message is displayed indicating that the number is a multiple of 10. - If the condition is
False
, theelse
block is executed, and a message is displayed indicating that the number is not a multiple of 10.
- If the condition is
π Output 1
Enter a number: 5
5 is not a multiple of 10.
π Output 2
Enter a number: 100
100 is a multiple of 10.
π Related Exercises from Chapter 7:
- ➤ Exercise 7-1 | Rental Car
- ➤ Exercise 7-2 | Restaurant Seating
- ➤ Exercise 7-3 | Multiples of Ten
- ➤ Exercise 7-4 | Pizza Toppings
- ➤ Exercise 7-5 | Movie Tickets
- ➤ Exercise 7-6 | Three Exits
- ➤ Exercise 7-7 | Infinity
- ➤ Exercise 7-8 | Deli
- ➤ Exercise 7-9 | No Pastrami
- ➤ Exercise 7-10 | Dream Vacation
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment