Exercise 7-10 Solution Python Crash Course Chapter 7 : User Input And While Loops

 

πŸ“˜ 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:

  1. Initialize an empty list:

    The variable dream_vacation is initialized as an empty list. This list will store the destinations provided by the users.

  2. 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.

  3. Store user input:

    Each destination entered by the user is appended to the dream_vacation list using the append() method.

  4. Check for continuation:

    After each input, the user is asked if they want to add another destination. If the user enters 'no', the flag variable is set to False, which stops the loop.

  5. 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.

  6. Display poll results:

    The program displays all the destinations stored in the dream_vacation list using a for loop. It also displays the user's favorite destination stored in fav.

πŸ” 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:


πŸ”— 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

“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