Search code examples
pythoncmdwhile-loop

What is the difference between opening a .py file from CMD and opening it directly from file explorer?


I have just started learning Python from the book 'Python Crash Course: 2nd edition' by Eric Matthes. I am using Windows. Most of the times, I open .py files directly by double-clicking on them from Windows file explorer.

But when I tried to run the following program in the same way, I found that it closed the cmd window immediately when the while loop stops even if it is supposed to continue:

#7.10 Dream Vacation (Small exercise to test what I have learnt)

dream = {}    #empty dictionary to store user inputs.
while True: 
    name = input('Enter your name: ')    #the key input for dream dictionary
    place = input('Enter your dream vacation destination: ')    #value input
    dream[name] = place    #Adding the input key and value pair to dream{} dictionary.

    #if the user wants to stop entering new inputs and wants to see the output:
    repeat = input('Do you want another person to respond? (yes/no): ')
    if repeat == 'no':    #if the user enters 'no' then the while loops stops looping.
        break

print("\nHere are the poll results:")
for name, place in dream.items():
    print(f"{name} wants to visit {place}.")

But when I run the same code from the traditional way of opening it properly from CMD (by using cd commands), the code runs exactly as it is supposed to.

I am curious about why this happens. Please let me know why do you think this might be happening. I am open to any suggestions or guidance from more experienced programmers like you all!


Solution

  • When your program ends the while loop, it actually does prints the stuff and the program exits, so basically it does that (prints the text) but you don't see it because the program exits and console closes, if you open it through CMD when the program exits the console doesn't closes and the text keeps on screen.