π Python Crash Course – Chapter 7: User Input And while loops – Exercise 7-10 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 7-10
Task:
Write a program that polls users about their dream
vacation . Write a prompt similar to If you could visit one place in the world,
where would you go? Include a block of code that prints the results of the poll.
✅ Solution Code:
dream_vacation = []
flag = True
while flag == True:
place = input("What is your dream vacation destination? : ")
dream_vacation.append(place)
repeat = input("Would you like to add another place? (yes/no) : ")
if repeat.lower() == 'no':
flag = False
fav = input("If you could visit one place in the world, where would you go? : ")
print("-----------------------------------------------------------------")
print("The results of the poll are:")
print(f"dream vacation-destination:")
for place in dream_vacation:
print(f"\t{place.title()}")
print(f"favourite destination:")
print(f"\t{fav.title()}")
print("-----------------------------------------------------------------")
π§ Code Explanation:
-
Initialize an empty list:
The variable
dream_vacation
is initialized as an empty list. This list will store the destinations provided by the users. -
Set up a loop for user input:
A
while
loop is used to repeatedly prompt the user for their dream vacation destination. The loop continues until the user indicates they do not want to add more destinations. -
Store user input:
Each destination entered by the user is appended to the
dream_vacation
list using theappend()
method. -
Check for continuation:
After each input, the user is asked if they want to add another destination. If the user enters
'no'
, theflag
variable is set toFalse
, which stops the loop. -
Ask for a favorite destination:
After exiting the loop, the user is asked to specify their favorite destination, which is stored in the variable
fav
. -
Display poll results:
The program displays all the destinations stored in the
dream_vacation
list using afor
loop. It also displays the user's favorite destination stored infav
.
π Output:
What is your dream vacation destination? : India
Would you like to add another place? (yes/no) : yes
What is your dream vacation destination? : china
Would you like to add another place? (yes/no) : yes
What is your dream vacation destination? : America
Would you like to add another place? (yes/no) : no
If you could visit one place in the world, where would you go? : Shimla
-----------------------------------------------------------------
The results of the poll are:
dream vacation-destination:
India
China
America
favourite destination:
Shimla
-----------------------------------------------------------------
π 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