How do I use raw_input in Python 3

后端 未结 8 2395
旧时难觅i
旧时难觅i 2020-11-22 01:39
import sys
print(sys.platform)
print(2**100)
raw_input()

I am using Python 3.1 and can\'t get the raw_input to \"freeze\" the dos pop-

8条回答
  •  旧巷少年郎
    2020-11-22 02:18

    Here's a piece of code I put in my scripts that I wan't to run in py2/3-agnostic environment:

    # Thank you, python2-3 team, for making such a fantastic mess with
    # input/raw_input :-)
    real_raw_input = vars(__builtins__).get('raw_input',input)
    

    Now you can use real_raw_input. It's quite expensive but short and readable. Using raw input is usually time expensive (waiting for input), so it's not important.

    In theory, you can even assign raw_input instead of real_raw_input but there might be modules that check existence of raw_input and behave accordingly. It's better stay on the safe side.

提交回复
热议问题