So first create a list of words..... I would start at 20 words, not too long.Create a program that prints a list of words in random order.
Then work out a way to know if a word has been printed or not, if a random choice picks the same number again, go choose another random number.The Program should print all the words and not repeat any...
Was trying to not give the full answers but suggest he learns by doing even if you do many times you can learn from that.jamesh wrote:Another way, create a list, then do a random shuffle on the list, then print it out.
Look up random module functions a lot simpler.keybeeper wrote:1. Put the words into a list (length is n).
2. Choose one at random and print it.
3. Replace the chosen word with the last word in the list. The list is now of length n-1.
4. Repeat steps 2 and 3 above until the list is exhausted.
You will need to check that you have not chosen the last word in the list and if so, it does not need replacing at step 3.
That's a great book, although my copy is at work at the moment.Bigcat123 wrote:Hi Guys,
I am having some very vain attempts to try and learn Python... And have been reading through Mike Dawson's 'Python Programming for the absolute beginner' (Thats me! )
I have got to page 155 (Hozar!) and I am attempting the challenges... This one has stumped me:
Create a program that prints a list of words in random order. The Program should print all the words and not repeat any...
I know that you are all going to think that this is easy.... I am A NOOB!
Please can someone help me?
Either use random to randomly sort the array then just print itBurngate wrote:Okay, I'm even more of a Python Noob than you - I've not even read the book!
But having read through this thread, I think I've come up with something different
So in BBC Basic (that I do everything in) I'd create two arrays, x wide where x is the number of words. 1st array A$(i) is a string array, second array N(i) is integer - fill it with its index i
Then a couple of nested FOR loops, in which you run through (x-1)*(x-1) times randomly swapping the N(i) entries
Lastly, print out the list in the order of the N(i) entries
Precisely easiest way and add an outer loop and you can prove the sequences are not the same by iterating many times, in other test it actually meets the requirements.billio wrote:Try this (python 2.7) :
mylist = ["red","white","blue","red"]
mylist = list(set(mylist))
for i in mylist : print i
These days I see too many people cut and paste answers and do not understand why.billio wrote:It's just a different way of learning - see an answer, work out why it works, think why that answer wasn't obvious in the first place. This isn't necessarily better.
Experience is directly proportional to equipment ruined.techpaul wrote:These days I see too many people cut and paste answers and do not understand why.billio wrote:It's just a different way of learning - see an answer, work out why it works, think why that answer wasn't obvious in the first place. This isn't necessarily better.
Often the attempts and getting it wrong help to understand, then do it again a better way.
Doing it is still best learning tool.
Users browsing this forum: Baidu [Spider] and 15 guests