By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

Question about text in Python

P: n/a
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

Apr 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Steve schrieb:
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.
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
Apr 2 '07 #2

P: n/a
On 4/2/07, Steve <st*******@removethisnowgmail.comwrote:
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

--
http://mail.python.org/mailman/listinfo/python-list
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.
Apr 2 '07 #3

P: n/a
On Mon, 02 Apr 2007 18:48:00 +1000, Steve wrote:
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?
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

Apr 2 '07 #4

P: n/a
Steve wrote:
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.
Homework?

Regards,
Björn

--
BOFH excuse #106:

The electrician didn't know what the yellow cable was so he yanked
the ethernet out.

Apr 2 '07 #5

P: n/a
Yes it is.

Where else to ask for help but here.

Thanks for your help everyone.

I figured it out.

"Bjoern Schliessmann" <us**************************@spamgourmet.comwro te
in message news:57*************@mid.individual.net...
Steve wrote:
>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.

Homework?

Regards,
Björn

--
BOFH excuse #106:

The electrician didn't know what the yellow cable was so he yanked
the ethernet out.
Apr 3 '07 #6

P: n/a
"Steve" <st*******@REMOVETHISNOWgmail.comwrites:
Yes it is [a homework question].

Where else to ask for help but here.
That's between you, your teacher, and your teaching institute's
plagiarism guidelines.

--
\ "I got a postcard from my best friend, it was a satellite |
`\ picture of the entire Earth. On the back he wrote, 'Wish you |
_o__) were here'." -- Steven Wright |
Ben Finney
Apr 3 '07 #7

P: n/a
On Tue, 03 Apr 2007 12:26:47 +1000, Ben Finney wrote:
"Steve" <st*******@REMOVETHISNOWgmail.comwrites:
>Yes it is [a homework question].

Where else to ask for help but here.

That's between you, your teacher, and your teaching institute's
plagiarism guidelines.

Plagiarism is a serious example of fraud, which is not only a crime but is
unethical as well. You don't need to be a student or an academic to commit
plagiarism. Anybody can commit plagiarism.

If we applied the plagiarism guidelines that university students labour
under to the rest of us, perhaps we'd be a little less quick to toss
accusations of plagiarism around so easily. If it is plagiarism for a
student to ask how to detect an empty line, then it is plagiarism for
any of us to ask how to do anything.

At the point that students are unable to ask for information because
they're accused of plagiarism, well, that's "a total collapse of critical
reasoning", to quote Professor Steven Dutch of the University of Wisconsin.

Citation: http://www.uwgb.edu/dutchs/PSEUDOSC/PlagShame.HTM
Accessed 2007-04-03.
If the Original Poster _accidentally_ sees a newsgroup post that answers
his question -- "Oh, so that's how you exit a loop in Python!" -- is that
plagiarism? Or is it only plagiarism if he _asks_ for the information
first? Does he have to spend the rest of his life as a Python coder giving
credit to J. Random Hacker for using "his" technique of "using break to
exit a while loop"?

None of this is to excuse students who try to have their homework done for
them, or well-meaning but foolish people who, in response to a simple
question end up providing an entire working piece of code, essentially
doing the student's homework for them. Plagiarism does exist, and it is
fraud. But I'm sickened and saddened to see how plagiarism has been
bastardised by academia, turning it from serious fraud to trivial re-use
of phrases and ideas, and even _learning things from anyone someone other
than your lecturer_.

That's what it has come to. If you ask "how do I detect the user has
entered an empty line in raw_input?", you're deemed to be committing
fraud. WTF? There's a gulf the size of the Atlantic Ocean between
_learning_ and _committing fraud_, and we're so frightened by the thought
that somebody might fraudulently claim another person's ideas (which begs
the question of whether people can own ideas) that we're prohibiting
learning except through a couple of narrowly approved channels.

If you think I'm exaggerating, I challenge you to read Professor Dutch's
pages on plagiarism:

http://www.uwgb.edu/dutchs/PSEUDOSC/PlagShame.HTM
http://www.uwgb.edu/dutchs/PSEUDOSC/PlagiarNonsense.HTM

I challenge you to try to work as a coder, whether professional or
amateur, under the same restrictions that we've been placing on students.
I can't count the number of times people write to comp.lang.python asking
"How do I transmogrify the frombulator?" And let's not even mention
copying code snippets posted on Usenet.

These things are not fraud. They are learning.

This is not a call to turn a blind eye for plagiarism, or to do students
homework for them. It's a plea for common-sense. We're not bound by
university guidelines, or universities' over-broad definition of
plagiarism, and we don't have to live by them. We are ethically bound not
to do student's homework for them -- but that doesn't mean we're bound to
refuse to answer their reasonable questions, or to treat those who are
looking for help as frauds _just because they are a student_.

--
Steven D'Aprano

Apr 3 '07 #8

P: n/a
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrites:
On Tue, 03 Apr 2007 12:26:47 +1000, Ben Finney wrote:
"Steve" <st*******@REMOVETHISNOWgmail.comwrites:
Yes it is [a homework question].

Where else to ask for help but here.
That's between you, your teacher, and your teaching institute's
plagiarism guidelines.

Plagiarism is a serious example of fraud, which is not only a crime
but is unethical as well. You don't need to be a student or an
academic to commit plagiarism. Anybody can commit plagiarism.

If we applied the plagiarism guidelines that university students
labour under to the rest of us, perhaps we'd be a little less quick
to toss accusations of plagiarism around so easily.
I agree with the severity of such an accusation, which is why I didn't
make it in this case.

--
\ "[T]he question of whether machines can think [...] is about as |
`\ relevant as the question of whether submarines can swim." -- |
_o__) Edsger W. Dijkstra |
Ben Finney
Apr 3 '07 #9

P: n/a
Steve wrote:
Yes it is.

Where else to ask for help but here.
http://www.catb.org/~esr/faqs/smart-....html#homework

Regards&Good luck,
Björn

--
BOFH excuse #247:

Due to Federal Budget problems we have been forced to cut back on
the number of users able to access the system at one time. (namely
none allowed....)

Apr 3 '07 #10

P: n/a
"Steven D'Aprano" <ste..e@REMOVEME.c....rce.com.auwrote:

8< --------
This is not a call to turn a blind eye for plagiarism, or to do students
homework for them. It's a plea for common-sense. We're not bound by
university guidelines, or universities' over-broad definition of
plagiarism, and we don't have to live by them. We are ethically bound not
to do student's homework for them -- but that doesn't mean we're bound to
refuse to answer their reasonable questions, or to treat those who are
looking for help as frauds _just because they are a student_.
I agree - It is not fraud if the OP says he is a student, and asks for help
on something that puzzles him.

It would be fraud if he gets a "full answer" here, and represents it as
"his own work".

It would also be stupid, as its a sure fire way of failing your
examinations.

Getting clarification on some point here is no different from looking it
up in a library, except that its more risky - some wag may lie to the
poor bugger...

- Hendrik
Apr 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.