How to stop command prompt from closing in python?

前端 未结 3 938
时光说笑
时光说笑 2020-12-10 07:29

I am very new to python.. I used the code

x = input(\" Hey what is your name \" )       
print(\" Hey, \" + x)  
input(\" press close to exit \")
         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 08:04

    On windows, it's the CMD console that closes, because the Python process exists at the end.

    To prevent this, open the console first, then use the command line to run your script. Do this by right-clicking on the folder that contains the script, select Open console here and typing in python scriptname.py in the console.

    The alternative is, as you've found out, to postpone the script ending by adding a input() call at the end. This allows the user of the script to choose when the script ends and the console closes.

提交回复
热议问题