简体   繁体   中英

Python: Execute a line by user input?

I am currently working on a game and I was wondering if there was any way to execute commands like a text file by user input?

I would like to make it where the text doesn't pop up all at once, but where you could do something like "Press any key to continue" and when they do that, the next wall of text appears. Any help is appreciated. Thanks.

The easy way is:

input('Press Enter to continue')

(in Python 3; raw_input instead in Python 2) but that will indeed wait for an Enter, AKA return, before continuing.

If you're really adamant about the any -key part you'll have to get "down and dirty". In Microsoft Windows only,

import msvcrt
def wait():
    msvcrt.getch()
print('Press any key to continue')
wait()

will work -- but it will fail on Linux or MacOSX; you'll need other approaches for those. So please let us know which platform(s) you need to support and we'll figure something out!-)

尝试在打印每面文本之间放置input("Press Enter to continue")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM