How to stop an execution in Python?

Asked

Viewed 15,896 times

1

I’m starting programming in Python and would like to know how to stop the program execution?

In the language C for example, there is the equivalent command system("pause"). If I open IDLE, more specifically the Python shell the program when finishing the window does not close. However, if I use the Python executable that is similar to CMD, the program will close when finished. What do I do to solve this problem?

  • system("pause"); is not a "C command" - it uses a system shell call to run another integer PROGRAM - which is Pause. (The same works in Python, as given in the answer) - but is equivalent to calling a keychain to break down your door every time you want to enter the house, instead of fixing the lock.

2 answers

5

Use the "input" function - it waits until the user type enter (it can type more text before pressing enter, but if you are not going to do anything with this text, it is indifferent):

input("Pressione <enter> para continuar")

The os.system("pause") runs another entire program - and means that the "Pause" program has to exist on your operating system, and it is something just from Windows: your Python program (or C) that would work on Linux, Mac, Android, becomes "windows only" just because of it. But above all, using the`system("pause"), even in C, is equivalent in programming, to calling a winch to take your car somewhere, with you inside, because the car is out of gas, instead of filling up.

The "input" function, on the other hand, is internal to Python, it doesn’t even need to import any module - and it only depends on the interaction of the Python Framework itself with the standard input of the program. (In C the correct would be to use the function fgets).

  • >>>>>>>>>>>>>> http://answall.com/a/69853/3635

  • Yes. But some demented person started to teach the one systen("pause") for C in colleges some time ago, instead of explaining what was the nature of stdin terminal - people have graduated and today find this "normal". It’s one of the most surreal things I’ve ever seen in computing.

  • Excellent explanation, could have made in the question I pointed out instead of answering in dup and point in the comment fields.

1

Try this, it will wait for you to type a key to continue

import os  
os.system("pause")
  • It worked, the code was efficient. I would like to thank you for your help and availability.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.