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

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

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.