954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pause in Python

I would like to know how to use system("pause") from C++ in Python 2.7 to get similar effect. Could you help me?

Catchamouse
Newbie Poster
7 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You can use

from time import sleep
sleep(1.5) # sleep 1.5 seconds

The precision of the sleep depends on your system (see timemodule.c for implementation details).

Also you may want to try

import os
os.system("pause")


Finally, if you want to wait until ENTER is pressed, simply use raw_input

raw_input("Press enter to continue")


This snippet could help you too http://snippets.dzone.com/posts/show/915

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Normally raw_input (Python 2) or input (Python 3) is enough. If you want to get fancy and have anyway in your module library my Tkinter getkey , you can utilize it also (for me input has been enough) You may want to make simple base class for it though as the snippet is reaction to key press for game use.

Notice also that many editors can pause the screen at end like ConText editor for Windows.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I have this code

if __name__ == "__main__":

    def make_index(input, output):
        f_input = open(input, "rt")
        f_output = open(output, "wt")
        splitting_chars = (' ','.','!','?','\n',',','"')
        char_buffer = ""
        row_index = 0
        dict = {}

        for row in f_input:
            row_index += 1
            for char in row:
                if char not in splitting_chars:
                    char_buffer += char
                else:
                    if char_buffer is not "":
                        if not dict.has_key(char_buffer):
                            dict[char_buffer] = []
                        if row_index not in dict[char_buffer]:
                            dict[char_buffer].append(row_index)
                    char_buffer = ""
     
        keys = dict.keys()
        keys.sort()

        for key in keys:
            s = reduce(lambda a,b: str(a) + " " + str(b), dict[key])
            f_output.write(key + " "  +  str(s) + " \n")

        f_input.close()
        f_output.close()

    def search_by_index(input, index):
        file = open(input, "rt")
        rows = file.readlines()
        file.close()
        dict = {}
        for row in rows:
            lst = row.split(" ")
            dict[lst[0]] = map(lambda a: int(a), lst[1 : len(lst)-1])

        if dict.has_key(index):
            return dict[index]
        else:
            return []

    make_index("input.txt", "index.txt")
    print search_by_index("index.txt", "Python")
    print search_by_index("index.txt", "Kreatrix")

    raw_input("Press enter to continue")


In this case raw_input or import os does not work. Could you help me?

Catchamouse
Newbie Poster
7 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: