π Python Crash Course – Chapter 7: User Input And while loops – Exercise 7-2 solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 7-2
Task:
Store a message in a variable, and then print that message.
✅ Solution Code:
members = int(input("How many people are in your dinner group? "))
if members > 8:
print("You'll have to wait for a table.")
else:
print("Your table is ready.")
π§ Code Explanation:
-
Prompt the user for input:
The
input()
function is used to ask the user how many people are in their dinner group. The input is converted to an integer using theint()
function and stored in the variablemembers
. -
Check the group size:
An
if
statement checks whether the number of members in the group is greater than 8. This condition determines whether a table is immediately available or if the group needs to wait. -
Display appropriate messages:
- If the group size is greater than 8, a message is displayed indicating that they will have to wait for a table.
- If the group size is 8 or fewer, a message is displayed indicating that their table is ready.
π Output 1
How many people are in your dinner group? 7
Your table is ready.
π Output 2
How many people are in your dinner group? 10
You'll have to wait for a table.
π 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