[Tutor] Lists and While Loops

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Mar 28 23:01:46 CEST 2012


On 28/03/2012 21:15, Emile van Sebille wrote:
> On 3/28/2012 11:53 AM Ricky Brown said...
>> So I have to write a program that reads a message from the user and
>> prints a new message
>
> ok --
>
>> that contains all the words from the original
>> message but in the same order without repeating any of them unless they
>> show up more than once in the original message.
>
> ?? doesn't this mean repeat the exact same message? IOW if the user
> suppies:
>
> This is is a test test
>
> the above rule would resuire you to output exactly the same input I
> think....
>
>
>>
>> What I have thus far looks like this:
>>
>> message = input("Your message:")
>
> so, "print message" does it...
>
> ... but I suspect your requirement isn't accurate.
>
> Emile
>
>

What the OP is asking for would output your original

This is is a test test

as

is is test test

I have a strong sense of Déjà vu, namely gathering requirements being 
far more difficult than writing code :)

I've no idea whether the requirement given is right or wrong.  Whatever 
the case I'd be using the built-in set or on older Pythons the sets module.

>
>
>> myList = message.split()
>> y = random.choice(myList)
>> z = len(myList)
>> while z !=0:
>> myList.remove(y)
>> print(y)
>>
>> I can't figure out
>> 1) How to remove "y" from the list and continue the loop; when I use
>> .remove etc. it runs once then give me the error that "y" is not in the
>> list.
>>
>> I imagine the answer is quite simple I'm just getting really frustrated
>> trying to get this done any advice is appreciated.
>>
>> Thanks
>>
>

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list