问题描述
我已经创建了一个Python程序,用户可以输入一行文本,然后
将创建该文本的首字母缩略词。
我想要添加到程序中的是重新运行此过程(用户输入另一行文本的b $ b $),直到用户有已经够了,
输入一个空白行,然后结束该程序。
任何人都可以帮忙吗?
谢谢
Hi,
I''ve created a Python program that a user enteres one line of text which
will then create an acronym from that text.
What I want to add to the program is the abilty to rerun this process (where
the user enteres another line of text) until the user has had enough and
enters a blank line which would then end the program.
Can anyone help with this?
thanks
推荐答案
您可以使用while循环执行此操作。如果用户输入一个空行你
可以突破循环:
而真实:
(部分代码)
如果不行:
休息
托马斯
You can do this with a while loop. If the user enters a blank line you
can break out of the loop:
while true:
(some code)
if not line:
break
Thomas
未经测试:
如果__name__ ==" __ main __":
userinp = raw_input(" Query>")
而userinp:
callmyabbrvfunction(userinp)
userinp = raw_input(" Another Query>")
print"退出:你有输入空字符串!
欢呼,
-
----
Amit Khemka - onyomo.com
主页:
无尽的世界轮回,无尽的太阳旋转,无尽的追求;
我再次回到我自己的开始,在这里,找到休息。
Untested:
if __name__ == "__main__":
userinp=raw_input("Query>")
while userinp:
callmyabbrvfunction(userinp)
userinp=raw_input("Another Query>")
print "Exit: You have entered empty string !"
cheers,
--
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world''s turn, endless the sun''s Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
这不是关于_text_的问题。这是关于编程
技术的问题。
首先,将任务分成小块:
*从用户那里获得输入(一行文字);
*从文本中做一个首字母缩略词;
*决定你是否已经完成;
*如果没有,请从头开始重复。
现在编写代码来完成每件作品。
现在你只需要do是创建执行每个子任务的函数:
def get_input():
"""从用户那里获取输入。 "
返回raw_input(请输入一个句子。)
def make_acronym(text):
"" ;返回给定文本的首字母缩略词。""
#你已经完成了这个
....做的东西......
返回首字母缩略词
def are_we_done():
"""返回一个布尔标志,指示是否
我们'做完首字母缩略词。
" ;""
print"你想要制作另一个缩写吗?
print"输入Y或y表示是,N或n表示否。
result = raw_input("然后按Enter键。 (y / n?)")
返回结果''yY''
现在把它们放在一起循环。
完成=假#我们尚未完成
未完成时:
#重复直到我们完成
text = get_input ()
首字母缩略词= make_acronym(文字)
打印首字母缩略词
done = are_we_done()
有很多其他方式这样做。这个(我认为)最容易理解,但还有其他方法更有效,但使用更高级的概念,例如异常。玩得开心
探索完成这项任务的不同方式。
-
Steven D''Aprano
It isn''t really a question about _text_. It''s a question about programming
techniques.
Firstly, break the task into small pieces:
* get input (a line of text) from the user;
* make an acronym from the text;
* decide if you''re done;
* if not, repeat from the beginning.
Now write code to do each piece.
Now all you need to do is create functions to do each sub-task:
def get_input():
"""Get input from the user."""
return raw_input("Please type a sentence. ")
def make_acronym(text):
"""Return an acronym from the given text."""
# you have already done this
.... do stuff ...
return acronym
def are_we_done():
"""Return a boolean flag indicating whether
we''re done making acronyms.
"""
print "Would you like to make another acronym?"
print "Type Y or y for yes, N or n for no."
result = raw_input("Then press the Enter key. (y/n?) ")
return result in ''yY''
Now put them all together into a loop.
done = False # we''re not done yet
while not done:
# repeat until we''re done
text = get_input()
acronym = make_acronym(text)
print acronym
done = are_we_done()
There are lots of other ways of doing this. This one is (I think) the
easiest to understand, but there are other ways that are more
efficient but use more advanced concepts like exceptions. Have fun
exploring the different ways of doing this task.
--
Steven D''Aprano
这篇关于关于Python中的文本的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!