Something to do with words

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

Something to do with words

Postby el_manu » Mon Aug 26, 2013 6:38 am

Code: Select all
b = 0
f = 0
while f <= 140:
   a = input("Words: ")
   b = len(a)


is there a way to store b(the length of a) after every input and store them under the variable of f????

so that you keep inputting until the total amount of words is less than or equal to 140
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby Mekire » Mon Aug 26, 2013 6:56 am

Could you elaborate on what you are trying to accomplish? The a;b problem is very common amongst people asking questions in programming forums. It refers to a person asking a question about their proposed implementation of a problem; rather than asking about the problem itself. Try to avoid falling into it.

You can certainly keep adding words to a list up until a given number. You could even allow the user to enter multiple words at once, if you split on spaces or commas.

Do you want 140 words, or a number of words equalling no greater than 140 characters?

-Mek
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 6:58 am

THE CODE ABOVE WON'T work.


the question is where you keep where you add one word or phrase at a time (You will never have a blank line as input, and each line of input will be less than 140 characters.), stopping and not including the last person's word (or words) that would take you over the 140 character limit.
then you must print out the whole phrases together.

an example of output is:

Words: Two
Words: far
Words: one
Words: both alike in dignity
Words: in fair Verona, where we lay our scene,
Words: decided
Words: when
Words: they
Words: would
Words: live angrily
Words: ever after
Words: like mouses
Words: craft
Two far one houses both alike in dignity in fair Verona, where we lay our scene, decided when they would live angrily ever after like mouses
Tweet length: 140



in the case above, the message is exactly 140 characters, and the next line is still asked for.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 7:00 am

Mekire wrote:Could you elaborate on what you are trying to accomplish? The a;b problem is very common amongst people asking questions in programming forums. It refers to a person asking a question about their proposed implementation of a problem; rather than asking about the problem itself. Try to avoid falling into it.

You can certainly keep adding words to a list up until a given number. You could even allow the user to enter multiple words at once, if you split on spaces or commas.

Do you want 140 words, or a number of words equalling no greater than 140 characters?

-Mek


you were right........i was only asking about my implemenation and it was WronG!
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby stranac » Mon Aug 26, 2013 7:19 am

I still don't quite understand your question.

Do you want the total length of all the inputs to be less than or equal 140, or just a single one?
Also, is it the number of words or letters?

If this is an assignment or something, I'd suggest just copy/pasting the entire thing here.
That would make understanding it much easier.
Friendship is magic!

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

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 7:25 am

i'm sorry i don't actually have the question written down in words as we were explained what the question was.

but each input has to be less than 140 characters and not a blank space. all inputs together have to be less than 140 characters as well, and you keep imputing until you get there.
then you print everything that has been inputted. if you have lets say 137 characters then you ask for another word and if the total count after that word has been counted is over 140 then you don't include that word.
then you need a line that counts the amount of characters in that sentence that you just printed
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 7:41 am

any tips to get me started?
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby Mekire » Mon Aug 26, 2013 7:57 am

Well i do believe you should only be allowing the user to enter single words at a time. If you allow for the user to enter multiple words divided by spaces it gets trickier. One issue you aren't taking into account though is that if the user enters individual words, there is also a blank space that must be added. This adds to the length of the string. You could append each word to a list and then use
Code: Select all
len(" ".join(word_list))
to find the length.

-Mek
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 8:33 am

would rstrip do anything???
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 8:40 am

Mekire wrote:Well i do believe you should only be allowing the user to enter single words at a time. If you allow for the user to enter multiple words divided by spaces it gets trickier. One issue you aren't taking into account though is that if the user enters individual words, there is also a blank space that must be added. This adds to the length of the string. You could append each word to a list and then use
Code: Select all
len(" ".join(word_list))
to find the length.

-Mek



by the way the length of the string is meant to include spaces if you look at my example above. the length includes commas spaces whatever. u name it. does that make it easier now??
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 10:25 am

Code: Select all
a = input("Words: ")
b = len(a)
c = 0
while b < 140 and c <= 140 and a != "":
  a = input("Words: ")

c = len(a)

my new attempt. is there a way to store the input words to form a sentence at the end and to have the count add those stored words???
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby Mekire » Mon Aug 26, 2013 10:57 am

You can get a list of the input words by splitting the input with:
Code: Select all
my_input.split()
You can extend a list of words with
Code: Select all
words.extend(my_input.split())

You can get the length of the entire constructed sentence with the previously shown join. If the sentence exceeds the limit, start popping words off the list.

-Mek
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 12:21 pm

and how do you use the quotes above in my case??
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Mon Aug 26, 2013 1:12 pm

Read below[b][/b]
Last edited by el_manu on Tue Aug 27, 2013 2:03 pm, edited 1 time in total.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby el_manu » Tue Aug 27, 2013 2:02 pm

Code: Select all
words = input("Words: ")
c = words
words = input("Words: ")
b = len(words)
while words != "" and b < 140 and len(c) < 140:
  c += " " + words
  words = input("Words: ")
print(c)
print("Tweet length: ", len(c))


what is my mistake here. it is not meant to print the last input in the sentence if it will take it over 140 words...help!!
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Something to do with words

Postby Mekire » Tue Aug 27, 2013 2:46 pm

The loop breaks once the string is already over the 140 limit. By the time you break out of your while loop you are already over the limit. I still recommend you keep the individual words in a list as this makes it easier to pop extra words off.

Something like this works, but there is a problem.
Code: Select all
sentence = ""
while True:
    words = input("Words: ")
    if not sentence:
        new_sentence = words
    else:
        new_sentence = "{} {}".format(sentence,words)
    if not words or len(new_sentence) > 140:
        break
    sentence = new_sentence
print(sentence)

This version will throw out all content in the last entry. If the entry was not just a single word then the amount cut off could be more than necessary.

It may be sufficient to your needs, but if it isn't then you should probably reevaluate your approach to use lists and join.

-Mek

Edit:
Fixed.
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Something to do with words

Postby el_manu » Wed Aug 28, 2013 5:54 am

thanx it helped a lot...it worked.


I won't bother you with my nonsense for a bit.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests