(2013年)我不知道为什么Python很奇怪,你不能通过谷歌搜索很容易找到这个,但它很简单.
如何检测"SPACE"或实际上是否有任何键?我怎样才能做到这一点:
print('You pressed %s' % key)
Run Code Online (Sandbox Code Playgroud)
这应该包含在python核心中,所以请不要链接与核心python无关的模块.
我正在创建一个Python脚本,要求从命令行输入.用户可以编辑文件的一部分.我可以要求提供新信息并在文件中覆盖它,没问题.但我宁愿将文件的编辑部分放在命令行中,因此不必完全输入.这可能吗?
文件:
1|This file
2|is not empty
Run Code Online (Sandbox Code Playgroud)
例:
>>>edit line 2
Fetching line 2
Edit the line then hit enter
>>>is not empty #This is written here by the script, not by the user
Run Code Online (Sandbox Code Playgroud)
然后可以改为
>>>is not full either
Edited file
Run Code Online (Sandbox Code Playgroud)
该文件已更改为:
1|This file
2|is not full either
Run Code Online (Sandbox Code Playgroud)
我希望我很清楚我想要完成什么.
据说这个问题在一定程度上回答了我的问题.当我运行Linux时,它会这样做readline.但是,我不是.我正在使用Windows而我没有使用readline.我想只使用标准库.
该问题还提供了Windows的答案.但是,我得到一个 ImportError有win32console,这可能是因为提到的问题不是Python3.4,但我是.另外,我想知道这是否可以使用标准库,而不是外部库.
所以,正如标题所说,我想要一个合适的代码来关闭我的python脚本.到目前为止,我已经使用过input('Press Any Key To Exit'),但是这样做会产生错误.我想要一个只关闭脚本而不使用错误的代码.
有没有人有想法?谷歌给了我输入选项,但我不希望它使用此错误关闭:
Traceback (most recent call last):
File "C:/Python27/test", line 1, in <module>
input('Press Any Key To Exit')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
Run Code Online (Sandbox Code Playgroud) 我在 UVa OJ 中遇到了这个问题。272-文本行情
嗯,问题很简单。但问题是我无法读取输入。输入以文本行的形式提供,输入结束由 EOF 指示。在 C/C++ 中,这可以通过运行 while 循环来完成:
while( scanf("%s",&s)!=EOF ) { //do something }
Run Code Online (Sandbox Code Playgroud)
这如何在 python 中完成。?
我在网上搜索过,但没有找到任何满意的答案。
请注意,输入必须从控制台读取,而不是从文件中读取。
我想在 python 中进行用户输入,这类似于c++ 中使用的getchar()函数。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ch;
while(1){
ch=getchar();
if(ch==' ') break;
cout<<ch;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输入:堆栈溢出
输出:堆栈
在上面的代码中,当用户输入一个空格时,循环会中断。我想使用我在 c++ 代码中使用的getchar()类型函数在 python 中执行此操作。
我正在编写一个程序,我希望光标在同一行上打印字母,然后删除它们,好像一个人正在打字,犯了错误,删回了错误,并从那里继续打字.
到目前为止,我所有人都能够在同一条线上写下它们:
import sys, time
write = sys.stdout.write
for c in text:
write(c)
time.sleep(.5)
Run Code Online (Sandbox Code Playgroud) amt = float(input("Please enter the amount to make change for: $"))
Run Code Online (Sandbox Code Playgroud)
我希望用户以美元输入金额,因此允许5个字符(00.00)有没有办法限制它,所以它不允许他们输入超过5个字符?
我不想要这样的东西,它允许你输入超过5但会循环.
while True:
amt = input("Please enter the amount to make change for: $")
if len(amt) <= 5:
print("$" + amt)
break
Run Code Online (Sandbox Code Playgroud)
我希望完全限制输入超过5个字符
我需要在Python中将标准输入切换到非缓冲模式,以便我可以读取它的单个字符.我设法让它工作,但现在标准输出被打破:不知何故,似乎在换行符后,发出一些空格字符,第一行为零,第二行为3,第三行为6,等等,这样:
ASD
ASD
ASD
Run Code Online (Sandbox Code Playgroud)
操作系统是Ubuntu Linux 12.04,64位版本,Python版本是3.2.3.
我怎样才能摆脱这种行为呢?
以下是我用过的代码:
import sys
import tty
import termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin)
for i in range(0, 10):
print("ASD")
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
Run Code Online (Sandbox Code Playgroud) 我试图用星号掩盖用户输入IDLE的内容,这样他们周围的人就无法看到他们输入/输入的内容.我正在使用基本的原始输入来收集他们输入的内容.
key = raw_input('Password :: ')
Run Code Online (Sandbox Code Playgroud)
用户输入密码后理想的IDLE提示:
Password :: **********
Run Code Online (Sandbox Code Playgroud) 我想要做的是用 Python 制作一个简单的 pi 记忆游戏。我需要的是一种无需在每个字符后按“输入”即可从用户那里获取输入的方法。听起来我需要像 getch 这样的东西,但我无法让它工作。我从这里得到了一个类似 getch 的函数:https : //gist.github.com/chao787/2652257#file-getch-py。我真的不明白里面的任何东西。当我做' x = getch.getch()'它说“ AttributeError: '_Getch' object has no attribute 'getch'”。看起来 msvcrt 可以在 Windows 上做到这一点,但我有一台 Mac。看起来curses 也是一个有getch 的东西,但它说我需要先做initscr,然后我得到错误“ File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/curses/__init__.py", line 30, in initscr
fd=_sys.__stdout__.fileno())
_curses.error: setupterm: could not find terminal”。
这是我只使用输入的文件,您每次都必须按 Enter 键(我实际上输入了 1000 位数字,而不是省略号)。
pi = '3.1415926535...'
def main():
print('Welcome to PiGame!')
pigame()
while True:
yn = input('Play again? y/n ')
if yn == 'y':
pigame()
else: return
def pigame():
n=0
print('Go!')
while …Run Code Online (Sandbox Code Playgroud)