问题
I've recently started learning python and got tired of running it on the command line (terminal mac os x). I wanted an environment that I could code nicely in and run that code only when pieces of it were done, not line-by-line as in the shell. I decided to use Xcode as the interface is clean and simple, and followed this tutorial to set up Xcode so that I could run python scripts.
My problem is that when I use raw_input() and then click run, I can't type a value to pass to the variable it's stored in. Take this simple line for example:
word = raw_input("Enter a word: ")
Later on in the program, word is printed. When I click run on Xcode, the prompt shows up as expected in the console:
Enter a word:
However, I can not type anything into it, the cursor is blinking so I know it is responding but when I type a value, nothing happens. I'm not sure what is wrong here, hopefully one of you can help me out.
回答1:
If you really want to use an IDE for Python development, go for Aptana Studio 3 .
Once you are done with Pydev setup which is very easy Refer this for Pydev Setup.
Before running a script which expects some command line inputs you can click on the small arrow button adjacent to run button, it will give you a drop down menu with different options. Click on run configurations and then click on a tab named Arguments and enter your parameters there in the field names Program Arguments. Click on apply and you are good to go.
Next time you need to run the code just click on run and it will automatically fetch arguments that you have specified in the option above.
Hope this help.
回答2:
You just need a text editor to store you Python script and run it in Terminal whenever you like. As an example, I use emacs. You may use vi, Sublime Text 2, TextWrangler or many other alternatives.
$ emacs a.py
word = raw_input("Enter a word: ")
print "Your word is: %s" % word
After the a.py file is saved, simply cd to the directory and run the script in Terminal.
$ python a.py
Enter a word: Hello
Your word is: Hello
Or import your script as a module under interactive mode.
$ python
>>> import a
Enter a word: Hello
Your word is: Hello
来源:https://stackoverflow.com/questions/18426722/python-raw-input-not-taking-input