使用raw_input()提供'default'值? [英] Providing 'default' value with raw_input()?

查看:76
本文介绍了使用raw_input()提供'default'值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


和往常一样,我的帖子都带有''警告:新手就在前方!''

免责声明...


我想知道是否有可能使用raw_input()来提供

''默认''值和提示?


我想包括使用DOS控制台Python应用程序编辑现有值(从SQLite表中绘制

)的能力,但我的直觉

通过阅读我能找到的关于raw_input()的感觉是它只有

允许你提供提示,而不是默认值。


如果有人能就我如何实现这一目标给我任何建议,我会非常感激
非常感谢!


非常感谢和热情,


planetthoughtful

Hi All,

As always, my posts come with a ''Warning: Newbie lies ahead!''
disclaimer...

I''m wondering if it''s possible, using raw_input(), to provide a
''default'' value with the prompt?

I would like to include the ability to edit an existing value (drawn
from an SQLite table) using a DOS console Python app, but my gut
feeling from reading what I can find about raw_input() is that it only
allows you to provide a prompt, not a default value as well.

If anyone can give me any advice on how I might achieve this, I would
be immensely appreciative!

Many thanks and much warmth,

planetthoughtful

推荐答案

planetthoughtful写道:
planetthoughtful wrote:
I我想知道是否可以使用raw_input()来提供
' 'default''值带有提示符?

def get_input_with_default(默认值,提示=">>> ):

result = raw_input(''[''+ str(默认)+'']''+ str(提示))
如果结果==
"":result = default

返回默认值

我想要包含编辑现有值(从SQLite表中绘制
)的功能一个DOS控制台Python应用程序,但我的直觉感觉从阅读我能找到的关于raw_input()的内容是它只允许你提供提示,而不是默认值。
I''m wondering if it''s possible, using raw_input(), to provide a
''default'' value with the prompt?
def get_input_with_default(default, prompt=">>> "):
result = raw_input(''[''+str(default)+''] ''+str(prompt))
if result == "": result = default
return default
I would like to include the ability to edit an existing value (drawn
from an SQLite table) using a DOS console Python app, but my gut
feeling from reading what I can find about raw_input() is that it only
allows you to provide a prompt, not a default value as well.



我认为编辑部分会给你带来困难。

curses和readline如果你在Unix平台上可能会有所帮助。 />
我无法想出在dos

提示符下提供可编辑值的方法。


一切顺利,

Keir。


I think it is the editing part that is going to give you a hard time.
curses and readline might be some help if you were on a Unix platform.
I can''t think of any way to provided a editable value at the dos
prompt.

All the best,
Keir.


planetthoughtful写道:
planetthoughtful wrote:
大家好,

和往常一样,我的帖子有一个''警告:新手就在前面!''
disclai mer ...

我想知道是否有可能使用raw_input()在提示符下提供
''default''值?

我想包括使用DOS控制台Python应用程序编辑现有值(从SQLite表中绘制)的能力,但我的直觉感觉从阅读有关raw_input的内容()是它只允许你提供提示,而不是默认值。

如果有人能就我如何实现这一点给我任何建议,我会
非常感谢和温暖,

planetthoughtful
Hi All,

As always, my posts come with a ''Warning: Newbie lies ahead!''
disclaimer...

I''m wondering if it''s possible, using raw_input(), to provide a
''default'' value with the prompt?

I would like to include the ability to edit an existing value (drawn
from an SQLite table) using a DOS console Python app, but my gut
feeling from reading what I can find about raw_input() is that it only
allows you to provide a prompt, not a default value as well.

If anyone can give me any advice on how I might achieve this, I would
be immensely appreciative!

Many thanks and much warmth,

planetthoughtful



您需要定义自己的功能,一个默认参数,给出

的值。如果没有提供默认值

,你可以使它像raw_input一样工作。类似于:


You need to define your own function, with a default argument that gives
the value. You can make it work just like raw_input if no default value
is provided. Something like:

def raw_default(prompt,dflt = None):
... if dflt :

... prompt ="%s [%s]:" %(提示,dflt)

... res = raw_input(提示)

...如果不是res和dflt:

.. 。返回dflt

...返回res

... raw_default(多少豆制作五个,5)
多少个豆制作五[5]:六个

''六''raw_default(多少豆制五个)
多少豆制成五个

' 'ten''raw_default(多少豆制作五个,5)
def raw_default(prompt, dflt=None): ... if dflt:
... prompt = "%s [%s]: " % (prompt, dflt)
... res = raw_input(prompt)
... if not res and dflt:
... return dflt
... return res
... raw_default("How many beans make five", ''5'') How many beans make five [5]: six
''six'' raw_default("How many beans make five") How many beans make fiveten
''ten'' raw_default("How many beans make five", ''5'')



有多少豆制作五个[5]:

''5''


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


How many beans make five [5]:
''5''

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


你好Steve,


正如keir所提到的,我的目标是能够从SQLite中存储的记录中编辑返回的值

,所以它可以用新的

值更新。


给一点背景:作为一个学习练习我正在写一个

命令行应用程序,它将允许我快速捕获待办事项

(基于类似于Mac上的应用程序使用的概念

43文件夹.COM)。我有功能添加待办事项,

和按各种标准列出项目,并更新

项目的状态,但没有包含任何功能能够编辑

项目。


根据kier的说法,这似乎无法通过
$ b $完成在DOS中命令行,这是一种耻辱。


对不起我在原帖中没有解释得很好。


非常温暖,


planetthoughtful

Hi Steve,

As keir mentioned, my goal is to be able to edit the value returned
from a record stored in SQLite, so that it can be updated with the new
value.

To give a little background: as a learning exercise I''m writing a
command line app that will allow me to quickly capture todo items
(based on a similar concept used by an app for the Mac described on
43folders.com). I have the functionality working for adding todo items,
and listing items by various criteria, and updating the status of
items, but haven''t included any functionality for being able to edit
items.

It seems, according to kier, that this simply can''t be done via the
command line in DOS, which is a shame.

Sorry I didn''t explain myself very well in my original post.

Much warmth,

planetthoughtful


这篇关于使用raw_input()提供'default'值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆