Need help with a task

This is the place for queries that don't fit in any of the other categories.

Need help with a task

Postby RookiePython123 » Thu Apr 03, 2014 4:26 am

Hello,

this is a homework question i am currently stuck on for my computing class. I have no experience with python before and this is my first months of using it. The assignment i am currently stuck on is this

-Write a program underscores.py that inputs a phrase from the user and prints out the phrase with the white space between words replaced by an underscore. For instance if the input is "the best one", then it would print the_best_one. The conversion can be done in only one or two statements using the recent string methods.

Online Tutorial Section 2.1.6.2, Acronym Exercise (Extra Credit, also 10 points)
An acronym is a string of capital letters formed by taking the first letters from a phrase. For example, SADD is an acronym for ‘students against drunk driving’. Note that the acronym should be composed of all capital letters even if the original words are not. Write a program acronym.py that has the user input a phrase (string) and then prints the corresponding acronym.
To get you started, here are some things you will need to do. First check that you understand the basic syntax to accomplish the different individual tasks: indicating the proper syntax using a Python function or operation will allow you to accomplish each task. Invent appropriate variable names for the different parts. This is not a complete set of instructions! The idea is to make sure you know the basic syntax to use in all these situations. See the questions after the list to help you put together the final program.
• Get the phrase (a string) from the user.
• Convert the phrase to upper case.
• Divide the phrase into words by creating a list of those words.
• Create a new empty list, letters. (Alternatively, this can be an empty string.)
• Go through the list created in #3 with a for loop, and get the first letter of each word.
• Append that first letter to the list letters. (Or, concatenate it to the string letters.)
• Join the letters together, with no space between them. (Skip this if letters is a string.)
• Print the acronym.

This is what I tried so far, but it separates every letter and not each word

Code: Select all
phrase = input("Please Enter a Phrase: " )
print('_'.join(phrase))
Last edited by stranac on Thu Apr 03, 2014 6:57 am, edited 2 times in total.
Reason: Locked OP. Merged two topics that were basically the same.
RookiePython123
 
Posts: 2
Joined: Thu Apr 03, 2014 4:18 am

Re: Need help with a task

Postby stranac » Thu Apr 03, 2014 6:59 am

'_',join() takes a sequence of strings, and puts '_' between every two of its items.
If you give it a string, the items are single characters, which is why you get the result you get.
You should give it a list of words instead.

Also, what problems are you having with the second one?
It seems pretty well explained.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need help with a task

Postby Kebap » Thu Apr 03, 2014 11:31 am

RookiePython123 wrote:This is what I tried so far, but it separates every letter and not each word

Code: Select all
phrase = input("Please Enter a Phrase: " )
print('_'.join(phrase))


Please try and do this:

Code: Select all
phrase = input("Please Enter a Phrase: " )
for part in phrase:
  print(part)


Soon, you will learn the debugging power of print. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Need help with a task

Postby snippsat » Thu Apr 03, 2014 2:59 pm

This is what I tried so far, but it separates every letter and not each word

Code: Select all
>>> phrase = input("Please Enter a Phrase: " ).upper()
Please Enter a Phrase: the best one
>>> phrase
'THE BEST ONE'
>>> phrase_lst = phrase.split()
>>> phrase_lst
['THE', 'BEST', 'ONE']
>>> '_'.join(phrase_lst)
'THE_BEST_ONE'
User avatar
snippsat
 
Posts: 138
Joined: Thu Feb 21, 2013 12:04 am

Re: Need help with a task

Postby RookiePython123 » Thu Apr 03, 2014 5:18 pm

Thank you guys so much. I finally figured a way in which it works.

Code: Select all
phrase = input("Please Enter a Phrase: ")
phrase = phrase.split()
print('_'.join(phrase))
RookiePython123
 
Posts: 2
Joined: Thu Apr 03, 2014 4:18 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], lolman8776, Shirtless and 3 guests