raw-input

How do I check if raw input is integer in python 2.7?

孤街醉人 提交于 2019-11-26 23:10:57
Is there a method that I can use to check if a raw_input is an integer? I found this method after researching in the web: print isinstance(raw_input("number: ")), int) but when I run it and input 4 for example, I get FALSE . I'm kind of new to python, any help would be appreciated. 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

Masking user input in python with asterisks

不想你离开。 提交于 2019-11-26 19:05:35
I am trying to mask what the user types into IDLE with asterisks so people around them can't see what they're typing/have typed in. I'm using basic raw input to collect what they type. key = raw_input('Password :: ') Ideal IDLE prompt after user types password: Password :: ********** Michael L Depending on the OS, how you get a single character from user input and how to check for the carriage return will be different. See this post: Python read a single character from the user On OSX, for example, you could so something like this: import sys, tty, termios def getch(): fd = sys.stdin.fileno()

How to let a raw_input repeat until I want to quit?

天大地大妈咪最大 提交于 2019-11-26 17:57:39
Say I want to use raw_input like this: code = raw_input("Please enter your three-letter code or a blank line to quit: ") under: if __name__=="__main__": How can I let it repeat multiple times rather than just once every time I run the program? Another question is to write what code can satisfy the condition "or a blank line to quit (the program)". 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))

Tab completion in Python's raw_input()

柔情痞子 提交于 2019-11-26 17:11:16
i know i can do this to get the effect of tab completion in python sure. import readline COMMANDS = ['extra', 'extension', 'stuff', 'errors', 'email', 'foobar', 'foo'] def complete(text, state): for cmd in COMMANDS: if cmd.startswith(text): if not state: return cmd else: state -= 1 readline.parse_and_bind("tab: complete") readline.set_completer(complete) raw_input('Enter section name: ') I am now interested in doing tab completion with directories. (/home/user/doc >tab) How would i go about doing such a task? Here is a quick example of how to perform incremental completion of file system paths

How do I check if raw input is integer in python 2.7?

大憨熊 提交于 2019-11-26 08:35:12
问题 Is there a method that I can use to check if a raw_input is an integer? I found this method after researching in the web: print isinstance(raw_input(\"number: \")), int) but when I run it and input 4 for example, I get FALSE . I\'m kind of new to python, any help would be appreciated. 回答1: 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)

Masking user input in python with asterisks

℡╲_俬逩灬. 提交于 2019-11-26 05:38:48
问题 I am trying to mask what the user types into IDLE with asterisks so people around them can\'t see what they\'re typing/have typed in. I\'m using basic raw input to collect what they type. key = raw_input(\'Password :: \') Ideal IDLE prompt after user types password: Password :: ********** 回答1: Depending on the OS, how you get a single character from user input and how to check for the carriage return will be different. See this post: Python read a single character from the user On OSX, for

Python: Problem with raw_input reading a number

我的未来我决定 提交于 2019-11-26 01:30:01
问题 unfortunately raw_input is not doing what I need it to do. What I am trying to do is get totPrimes = whatever I type in at the prompt. If i replace while count < totPrimes with while count < 50 this script works. If I type 50 into the prompt, this script doesnt work, I\'m afraid raw_input isn\'t the function im looking to use? Here is a snippet of my code: testNum = 3 div = 2 count = 1 totPrimes = raw_input(\"Please enter the primes: \") while count < totPrimes : while div <= testNum : 回答1:

raw_input and timeout [duplicate]

一世执手 提交于 2019-11-26 00:54:37
问题 This question already has answers here : Keyboard input with timeout? (11 answers) Closed 4 years ago . I want to do a raw_input(\'Enter something: .\') . I want it to sleep for 3 seconds and if there\'s no input, then cancel the prompt and run the rest of the code. Then the code loops and implements the raw_input again. I also want it to break if the user inputs something like \'q\'. 回答1: There's an easy solution that doesn't use threads (at least not explicitly): use select to know when

How to set time limit on raw_input [duplicate]

浪尽此生 提交于 2019-11-25 22:47:34
问题 This question already has an answer here: Timeout on a function call 14 answers in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped? 回答1: 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

Get a list of numbers as input from the user

試著忘記壹切 提交于 2019-11-25 22:26:47
问题 I tried to use raw_input() to get a list of numbers, however with the code numbers = raw_input() print len(numbers) the input [1,2,3] gives a result of 7 , so I guess it interprets the input as if it were a string. Is there any direct way to make a list out of it? Maybe I could use re.findall to extract the integers, but if possible, I would prefer to use a more Pythonic solution. 回答1: In Python 3.x, use this. a = [int(x) for x in input().split()] Example >>> a = [int(x) for x in input()