Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Problem with Python Loops: `for` and `while`

$
0
0

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.

# Empty dictionary where we'll store the results of the poll.poll = {}polling_active = Truewhile polling_active:    prompt_1 = "\nWhat is your name? "    response_1 = input(prompt_1)    prompt_2 = "If you could visit one place in the world, where would you go? "    response_2 = input("Hello" +" " + response_1 +"!" +" " + prompt_2)    # store name/place in dictionary.    poll[response_1] = response_2    prompt_3 = "Would you like to let another person respond? (yes/no) "    response_3 = input(prompt_3)    while response_3 == 'yes':#    if response_3 == 'no':          polling_active = Trueprint(f"\n {poll}")

I have noticed that the program does not compile in that case (I mean when the line if response_3 == 'no': is commented out). However, if I uncomment it and comment out the line while response_3 == 'yes':, it works perfectly. I am very confused about this. Can anyone explain to me why the while loop does not give the desired result here? Perhaps I do not understand the real difference between the for and while loops.

I have spent about 30 minutes trying to solve this problem, and I have finally solved it. However, I cannot understand why the while loop gives an error here, while the for loop works perfectly. Apologies if my question seems too simple, as I only started coding about a week ago.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>