User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 3:29 pm

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?
Just a beginner sharing his experiences on his way to geek nirvana...

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 3:36 pm

Break the problem down
Create a program that prints a list of words in random order.
So first create a list of words..... I would start at 20 words, not too long.
Then create a programme to print them in random order.
The Program should print all the words and not repeat any...
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.

Hint you know the size of the list so when only two words left print the chosen by random one then just print the last one.

Attack the programme in stages, get each stage working leave comments in describing the stages left to do.

The best programming tools are

1/ Piece of Paper
2/ Pencil or pen
3/ The mind

(you could use sand and stick or even blackboard and chalk if you wish :D )
Write out the design/flow in words first of all, then further design each stage
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Mortimer
Posts: 909
Joined: Sun Jun 10, 2012 3:57 pm

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 3:43 pm

Is it possible to store the words in a list. Randomly select a entry from the list, print it and then remove it from the list. Repeat until the list is empty?

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 4:02 pm

In addition to the three tools mentioned by techpaul the relevant documentation is also very helpful. There is a function in the `random` module which helps with bringing the words in the list into random order.

Edit:

Documentation: http://docs.python.org/2/library/random.html
Last edited by BlackJack on Wed Nov 14, 2012 4:21 pm, edited 1 time in total.

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
pstorey
Posts: 52
Joined: Wed Sep 19, 2012 11:14 am

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 4:06 pm

Also being a newbie i found this resource useful..

Will get you on the right track for your list of words..

http://docs.python.org/2/tutorial/datastructures.html

Cheers

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2827
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 4:58 pm

I was thinking use a list as well.
Then choose one at random print it and delete it from the list.
I'll shut up now before I give you it all on a plate. :lol:

Pretty much what Mortimer said. I agree :D
Alex Eames RasPi.TV, RasP.iO

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 5:06 pm

There are many ways to do this, the 'neatest' and minimal code way is what Blackjack hinted at.

As with all programming there are many ways to do it, often there is no single RIGHT way, there are plenty of good ways and plenty of OK ways, but there are always lots and lots of really bad ways :-)

try a method see if you can find a simpler method, often revisiting code can find better ways of doing it.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20946
Joined: Sat Jul 30, 2011 7:41 pm

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 5:06 pm

Another way, create a list, then do a random shuffle on the list, then print it out.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 5:08 pm

jamesh wrote:Another way, create a list, then do a random shuffle on the list, then print it out.
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.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Bigcat123
Posts: 230
Joined: Thu Aug 23, 2012 2:41 pm

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 5:26 pm

Wow... Thanks for all your replies!

@jamesh How would I 'reshuffle' a list? I have been experimenting with reversing but I still have no idea what to do!

Thanks Guys... I appreciate it!
Just a beginner sharing his experiences on his way to geek nirvana...

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 6:09 pm

@Bigcat123: Again: Look through the functions in the `random` module. Look at each function in the documentation of the module, read the description, think if it might be useful for the task at hand, and try to understand the example code.

Code: Select all

while not self.asleep():
    sheep += 1

keybeeper
Posts: 26
Joined: Wed Aug 08, 2012 10:15 am

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 11:20 pm

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.
No Microsoft products were used in the creation of this message

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Wed Nov 14, 2012 11:57 pm

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.
Look up random module functions a lot simpler.

Problem with your method you must remember that every time you select a random number the range has to change to between 0 and n-1, and statistically this will give you a lower probability of never repeating sequence.

Actually in python you have list remove methods so you dont need to move anything. The length is reduced automatically.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
paddyg
Posts: 2244
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 12:34 am

in some ways using python for this exercise is cheating. If you look hard enough you will probably find something that does the whole thing in one step!

Generally speaking wading through (or searching) the documentation is not as productive as using google which will often take you to stackoverflow (or here even), you can then return to the documentation to see how to use the functionality people suggest.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

SiriusHardware
Posts: 494
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 1:00 am

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?
That's a great book, although my copy is at work at the moment.

What I do remember is that the challenges are always at the end of a chapter, and they always involve a modest rearrangement or expansion of the code built up throughout the chapter. You should really already have everything you need to solve the problem. That's the way that book is written.

Look back through the chapter again and try to figure out how what you learn in it could be used to solve the challenge. If we help you too much, it will defeat the object of the challenge (although we do, of course, always try to help anyone who is utterly and completely stuck).

But in general...

First, just create a list of words (say twenty) and print them out one by one in the order in which they are stored. That tests that your list is good.

Then make a way to pick words out of the list in a random order until twenty have been displayed. Don't worry about whether they have already been picked. At this stage some words will be picked more than once and some not at all.

Next, each time you pick a random word from the list, mark it in some way to signify that it has already been picked. The easiest way might be to delete each word from the list as you pick it so it can't be picked again, until eventually the list is empty.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 8:51 am

There's a list.pop() thing you can do that returns an item and deletes it from the list at the same time...
note: I may or may not know what I'm talking about...

User avatar
Burngate
Posts: 5526
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 11:37 am

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

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 11:47 am

Burngate 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
Either use random to randomly sort the array then just print it

Or knowing length of array using a range option on for loop create an array of index numbers create a method to random sort the indexes then print word array by scanning indexes array.

Or in loop grab random number based on length of words array, print word remove (using pop) grab next random number using length unless length = 1 just print last word. Means words array is only populated at inital run cannot be rerun without reloading.

Or create an array of length of words array long fill each element with one, iniutialise a count to length of words array in a loop grab random numbers check if that index is 1 then print word change index to 0 and decrement count, check if count > 1 grab next random number repeat check and print, otherwise when count = 1 print it.

Those are just the ways off the top of my head.

Generally easier than doing other methods and keeping
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
Burngate
Posts: 5526
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 12:02 pm

So many cats, so little time to skin 'em

billio
Posts: 60
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 9:15 pm

Try this (python 2.7) :

import random
mylist = ["red","white","blue","red"]
mylist = list(set(mylist))
random.shuffle(mylist)
for i in mylist : print i

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 9:42 pm

billio wrote:Try this (python 2.7) :

import random
mylist = ["red","white","blue","red"]
mylist = list(set(mylist))
random.shuffle(mylist)
for i in mylist : print i
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.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
paddyg
Posts: 2244
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 10:35 pm

Err... what was the point of everyone else previously NOT just blurting out what the obvious solution was?!! Hopefully Bigcat followed Blackjack's hints and figured this out from the manual, learning lots in the process.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

billio
Posts: 60
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Thu Nov 15, 2012 11:09 pm

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.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Fri Nov 16, 2012 12:42 am

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.
These days I see too many people cut and paste answers and do not understand why.

Often the attempts and getting it wrong help to understand, then do it again a better way.

Doing it is still best learning tool.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
croston
Posts: 684
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PYTHON NOOB ALERT.... Please Help Me.... PLEASE

Fri Nov 16, 2012 12:46 am

techpaul wrote:
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.
These days I see too many people cut and paste answers and do not understand why.

Often the attempts and getting it wrong help to understand, then do it again a better way.

Doing it is still best learning tool.
Experience is directly proportional to equipment ruined.

Return to “Python”