NameError: name ‘raw_input’ is not defined [duplicate]
For Python 3.x, use input(). For Python 2.x, use raw_input(). Don’t forget you can add a prompt string in your input() call to create one less print statement. input(“GUESS THAT NUMBER!”).
For Python 3.x, use input(). For Python 2.x, use raw_input(). Don’t forget you can add a prompt string in your input() call to create one less print statement. input(“GUESS THAT NUMBER!”).
raw_input returns a string. So use int(raw_input()). And for how string and int comparsions work, look here.
Since the Python 2.x version of input() is essentially useless, you can simply overwrite it by raw_input: try: input = raw_input except NameError: pass In general, I would not try to aim at code that works with both, Python 2.x and 3.x, but rather write your code in a way that it works on 2.x … Read more
isinstance(raw_input(“number: “)), int) always yields False because raw_input return string object as a result. Use try: int(…) … except ValueError: number = raw_input(“number: “) try: int(number) except ValueError: print False else: print True or use str.isdigit: print raw_input(“number: “).isdigit() NOTE The second one yields False for -4 because it contains non-digits character. Use the second … Read more
best: if __name__ == ‘__main__’: while True: entered = raw_input(“Please enter your three-letter code or leave a blank line to quit: “) if not entered: break if len(entered) != 3: print “%r is NOT three letters, it’s %d” % (entered, len(entered)) continue if not entered.isalpha(): print “%r are NOT all letters — please enter exactly … Read more
Here is a quick example of how to perform incremental completion of file system paths. I’ve modified your example, organizing it into a class where methods named complete_[name] indicate top-level commands. I’ve switched the completion function to use the internal readline buffer to determine the state of the overall completion, which makes the state logic … Read more
If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput module: pip install pwinput Unlike getpass.getpass() (which is in the Python Standard Library), the pwinput module can display *** mask characters as you type. Example usage: >>> pwinput.pwinput() Password: ********* ‘swordfish’ >>> pwinput.pwinput(mask=’X’) # Change … Read more
There’s an easy solution that doesn’t use threads (at least not explicitly): use select to know when there’s something to be read from stdin: import sys from select import select timeout = 10 print “Enter something:”, rlist, _, _ = select([sys.stdin], [], [], timeout) if rlist: s = sys.stdin.readline() print s else: print “No input. … Read more
Do totPrimes = int(totPrimes) while count < totPrimes: # code raw_input gives you a string you must convert to an integer or float before making any numeric comparison.
The signal.alarm function, on which @jer’s recommended solution is based, is unfortunately Unix-only. If you need a cross-platform or Windows-specific solution, you can base it on threading.Timer instead, using thread.interrupt_main to send a KeyboardInterrupt to the main thread from the timer thread. I.e.: import thread import threading def raw_input_with_timeout(prompt, timeout=30.0): print(prompt, end=’ ‘) timer = … Read more