[Tutor] New to this list ....
Hi there I've just joined this list and thought I'd introduce myself. I used to be fairly competent in c but never made the grade to c++. I've done very little programming in the last couple of years or so. I'm getting a Raspberry-pi for our local Junior school and am starting to learn Python so I can show the year five and year six kids how to write simple games. I'm already enjoying this lovely language. One of the few c things I miss is the switch/case statement. if and elif in it's place is a bit cumbersome. Still, it works. One of the things I wanted to do is to use a four integer array to get four integers returned from a function. I ended up using what I think is a list. (I'm not really sure of the datatypes yet). This is what I did, and it works, but looks very inelegant to me: correct = 0 match = 0 wrong = 0 results = [correct, match, wrong] results = getflag(flag_1, results) results = getflag(flag_2, results) results = getflag(flag_3, results) results = getflag(flag_4, results) Any thoughts? Kind regards,Barry. -- From Barry Drake - a member of the Ubuntu advertising team ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to this list ....
On 30/03/12 15:04, Barry Drake wrote: One of the things I wanted to do is to use a four integer array to get four integers returned from a function. I ended up using what I think is a list. (I'm not really sure of the datatypes yet). This is what I did, and it works, but looks very inelegant to me: correct = 0 match = 0 wrong = 0 results = [correct, match, wrong] results = getflag(flag_1, results) results = getflag(flag_2, results) results = getflag(flag_3, results) results = getflag(flag_4, results) Sorry - I meant three-digit array, and the indents in the code fragment above were all in line originally. -- From Barry Drake is part of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to this list ....
On 30/03/12 16:19, Evert Rol wrote: Not sure. In the sense that you can "optimise" (refactor) it in the same way you could do with C. Eg: results = [0, 0, 0] flags = [0, 1, 2, 3] for flag in flags: results = getflag(flag, results) That's exactly what I hoped for. I hadn't realised I can initialise a list in one go - it seems that lists work a lot like the arrays I was used to in c. Thanks to the others who took the time to answer. Just now, Asokan's solution is a bit obscure to me - I'll work on that one, but the above is lovely and elegant; and easy to understand. Someone asked about the getflag function - it is: def getflag(thisflag, results): if (thisflag == 2): results[0] += 1 elif (thisflag == 1): results[1] += 1 elif (thisflag == 0): results[2] += 1 return(results) In c, I would have used switch and case, but I gather there is no direct equivalent in Python ... But it works as is. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to this list ....
On 30/03/12 17:58, Mark Lawrence wrote: The recipe here http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/ refers to several other recipes which you might want to take a look at, sorry I meant to mention this earlier. Oh, that's neat. Not worth putting into my little project, but I've bookmarked that on for when I need a lot of cases. It also shows how to define a class - that was something I had wondered about, but not yet tackled. I'm really grateful for the answers I have received. It will take me a while to get my head around some of the less obvious code fragment that folk have kindly posted, but I will play around with all of them in due course. Already the code I am playing with has shrunk to about half the number of lines it was before I joined this list and I'm even more than ever impressed with the language. I also like the fact that it can be played with as an interpreted language. Compiling and debugging c code could be a pain at times. In Python, a few print statements together with the syntax error messages from the interpreter make it very easy to see what's happening. Kind regards, Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to this list ....
On 30/03/12 17:22, Prasad, Ramit wrote: Unlike C, the parenthesis in if statements and returns are not necessary. Furthermore, the way Python binds names means that modifying the list in getflags modifies it in the callee. No need to return and reassign results. This is lovely. It's so much friendlier than c. I'm used to c variables going out of scope once you leave the called function. I imagine if you want to leave the variables unchanged, you have to re-assign them inside the function. I quite like it that way. Be careful of "results = [0] * 3". This style works fine for immutable types (int, float, str) but does not work as people new to Python think it does. Thanks for the warning. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to this list ....
On 30/03/12 19:18, Cranky Frankie wrote: Here's what you need - he starts simple and winds up with some nice games: http://www.amazon.com/Python-Programming-Absolute-Beginner-Edition/dp/1435455002/ref=sr_1_6?ie=UTF8&qid=1333131438&sr=8-6 Wow! I found an e-book copy online and got it. Looks good! I've looked at 'Snake Wrangling for Kids'. Informative, but the example code is not going to inspire kids. Al Sweigarts two e-books are a bit better, but I think I'm going to get more mileage from the above. Thanks. Kind regards, Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] breeds of Python .....
After following the reading suggestions, I soon found myself looking at quite a few code examples that would only run under a particular version of python. Finally, I converted the example that I was working on to run under Python3. I just wondered if you guys would advise a newbie like me to concentrate on Python3 or stay with Python2 and get into bad habits when it comes to change eventually? Apart from the print and input functions, I haven't so far got a lot to re-learn. Kind regards,Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 06:19, Brett Ritter wrote: My recommendation is to go with Python2 - most major projects haven't made the switch and I'd expect another year or two before they do so. Many tutorials and examples are Python 2-based and there are not that many differences to unlearn in terms of habits. Thanks Brett and those who replied. As my only reason for getting into Python is to be able to show the kids at the local school a bit about programming, and as I've no investment in existing code at all, I'm going to go with Python3. The tutorials and examples I have are as plentiful in Python3 as in Python2, and the ones I might want from the Python2 tutorial will be easy to convert and will help the learning process. The main reason I asked the opinion of this list was in case there was a vast opinion gap like there is in Ubuntu between Unity lovers and Unity haters. I guess Unity is a bit like Marmite. I get the view that Python3 is just a natural progression. I never experienced this with c as the standard library base on Kernighan and Ritchie never seemed to change its syntax from the word go. Kind regards,Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 12:03, Leam Hall wrote: For that path I'd agree that Python 3 is the way to go. I believe PyGame is Python 3 ready so you've got an automatic hook for the kids. Heck, probably many of their parents as well! Check out the book "More Python programming for the absolute beginner" as it teaches Python and PyGame at the same time. I've played around with PyGame on Python2 - hadn't realised it was ready for Python3 yet. It's just the kind of thing that would have sparked my son off when he was a kid. He wrote hundreds of lines in the rather dumb Basic that the Speccy used in the olden days, and guess what - when he went to uni, his degree was in computer science! I really hated Basic, and programmed in Z80 assembler until I met with c and learned how much fun programming could really be. Python is even more fun. I was a bit taken aback a few years later when my son left his job as sys-admin for a big firm. He said that the work was a job for a twenty-year old whiz-kid. He was more interested in how business works. He now charges an absolute fortune as a freelance consultant. Kind regards,Barry -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 12:03, Leam Hall wrote: I believe PyGame is Python 3 ready so you've got an automatic hook for the kids. Heck, probably many of their parents as well! PyGame is available for Python3 but not pre-built from the Ubuntu or Debian repos as far as I can see. I got the source from the PyGame site and built it. Note that the required c headers to build it are not in the standard install of Python3, so I had to get the matching source package and manually put the headers into the appropriate place. After that, it seems to build and work OK, and the PyGame examples are fun and helpful. I now await my Raspberry-pi to see what stuff I can run on it. I assume it comes with Python3 in the bootable Fedora OS. By the time it comes, I thinnk I'll have found my way around Python to a usable extent. Regards,Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 15:26, Alan Gauld wrote: Actually the standardization of C sparked huge debates in the early 90's. There were lots of minor changes and one big style change that really polarised opinions. In traditional C you defined a functions parameters like int foo() int a; float b; { /* body here */ } I started with c in the 1980s using Mix Power C under Microsoft DOS 3.5. It was a number of years before I finished up with GCC under Linux. Power-C was the only version of c I worked with for several years. The input parameters were always inside the function brackets in that version, so it must have been ansi-c. I hadn't realised it was any different from the K&R specs. Interesting! -- From Barry Drake (The Revd) Health and Healing advisor to the East Midlands Synod of the United Reformed Church. See http://www.urc5.org.uk/index for information about the synod, and http://www.urc5.org.uk/?q=node/703 for the Synod Healing pages. Replies - b.dr...@ntlworld.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 16:57, Alan Gauld wrote: Oops, a slight mistake there it should be: int foo(a,b) int a; float b; { /* body here */ } Ah, now that rings bells It's all a very long time ago, but I think my Power-C was able to accept either format and not complain. I still have my Power-C carefully preserved on a CD. I'll have to dig it out sometime and try it under DosBox emulator. That seems to run most ancient DOS stuff. I think my version of Power-C came on a 5-1/4" floppy and would run from there. I seem to remember using it before I had a computer with a hard-drive, just two 5-1/4" floppies. Those were the days! Regards,Barry. -- From Barry Drake - a member of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On 01/04/12 18:16, Stefan Behnel wrote: I don't see a major reason for a beginner to not go straight for Python 3, and then learn the necessary Py2 quirks in addition when the need arises. Thanks for that. Really re-assuring. Also, I hadn't looked at 2to3 until you mentioned it - and certainly I hadn't realised that I already have it as part of python3. Most of the simple examples that I would want to use should convert using 2to3 'out of the box'. Thanks. Regards, Barry. -- From Barry Drake - a menber of the Ubuntu advertising team. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Struggling with logic .....
Hi there Some months ago I decided to have a crack at Python. I set myself the task of coding the 'Mastermind' game and got into great problems where the random generated number contained duplicated digits. I recently decided to get back to it as I have volunteered to introduce the older kids at the local junior school to programming i initially using 'Scratch' and then touching on Python. I found some Mastermind example coding on the internet and took a look. I'm playing with the attached: It seemed to work OK so I re-wrote the input code to be (IMO) more logical, and added more text to make it obvious what is happening. Then I noticed it doesn't get the scoring right when there are duplicate digits! I'm no expert, so I wonder if you guys can explain in simple terms what is happening. I have to say I feel a bit stupid here. Below is one of the 'mistakes': $ python mastermind_2.py I have chosen a random four digit number. You have to guess what it is. Give me your guess at the four digit number . Enter four digits between 1 and 6: 1234 line is: ['1', '2', '3', '4'] Length: 4 Random Code: (3, 4, 2, 3) Result: 0 - you have a correct number in an incorrect position Result: 0 - you have a correct number in an incorrect position Result: 0 - you have a correct number in an incorrect position Result: -1 - you have an incorrect number I have chosen a random four digit number. You have to guess what it is. Give me your guess at the four digit number . Enter four digits between 1 and 6: 4215 line is: ['4', '2', '1', '5'] Length: 4 Random Code: (3, 4, 2, 3) Result: 0 - you have a correct number in an incorrect position Result: 0 - you have a correct number in an incorrect position Result: -1 - you have an incorrect number Result: -1 - you have an incorrect number -- Barry Drake is a member of the the Ubuntu Advertising team. http://ubuntu.com/ import random # generate random code code = (random.randrange(1, 6), random.randrange(1, 6), random.randrange(1, 6), random.randrange(1, 6)) line= ['','','','']# line from user cc = []# code count cl = []# line count matched= 0# number of matched items result = [-1, -1, -1, -1] # result user_own= False max_attempts= 10# XXX How to define a constant variable? attempts= 0 while not user_own and attempts < max_attempts: #line = raw_input(str(attempts + 1) + " You : ").strip().split(" ") # originally # Barry print "I have chosen a random four digit number. You have to guess what it is." input_str = str(input("Give me your guess at the four digit number . Enter four digits between 1 and 6: ")) for i in range(len(input_str)): line[i] = input_str[i] print "line is: ", line, " Length: ", len(line) # debug hint print "Random Code: ", code # for debugging only # /Barry if input_str == "": # cheat code for debugging print "Random Code: ", code if len(line) != 4: # won't be considered an attempt print "Please enter only 4 digits " continue # convert list members in integer line = [int(l) for l in line] # TODO check for color in range 1 to 6 # game has 6 colors cc = [0, 0, 0, 0, 0, 0] cl = [0, 0, 0, 0, 0, 0] matched = 0 for i in range(len(line)): if line[i] == code[i]: # matched guess matched += 1 else: cc[code[i] - 1] += 1 cl[line[i] - 1] += 1 if matched == 4: user_own = True continue # user is not own, evaluate user guesses i = 0 result = [-1, -1, -1, -1] while i < matched: # color matched along with position result[i] = 1 i += 1 ri = i for i in range(len(cc)): x = min(cc[i], cl[i]) for i in range(x): # color matched, position incorrect result[ri] = 0 ri += 1 if ri == 4: break; # XXX Note comma at the end to disable newline # print "Result:", for i in result: print "Result:", i, if (i == 1): print " - you have a correct number in a correct position" if (i == 0): print " - you have a correct number in an incorrect position" if (i == -1): print " - you have an incorrect number" attempts += 1 print if user_own: print "Congratulations - you have WON!!!" else: print "Sorry, YOU LOST!!!" print "Random Code was:", code ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struggling with logic .....
On 19/01/13 14:33, Alan Gauld wrote: line is: ['1', '2', '3', '4'] Length: 4 Random Code: (3, 4, 2, 3) Result: 0 - you have a correct number in an incorrect position Result: 0 - you have a correct number in an incorrect position Result: 0 - you have a correct number in an incorrect position Result: -1 - you have an incorrect number Looks good to me line is: ['4', '2', '1', '5'] Length: 4 Random Code: (3, 4, 2, 3) Same here. Sooo... Some comments on the code. Thanks. I seem to have misunderstood what the original coder had intended. I suppose the above output is making sense after all. OK, at this point I give up, this is way too complicated for what you are doing. I guess this is exactly why I have been struggling to understand some of the logic in the original code! I like your idea of sets of tuples for input and solution - I'll play around with the idea. Also, I think I want to alter the random initialisation - I don't like the way in which four separate random digits each with a range of only six are generated. I think generating a four digit number and splitting the digits will be a better option. As you mention, the use of colours in the original code is a distraction - I think that is the reason for limiting the digits to numbers between 1 and 6 - simply based on the old board-game. There's no other logical reason so I'll do away with that concept. Thanks again for your time. I'm GOING to learn to code Python! I used to code in C many years ago and never made the jump to C++, but Python has much nicer features for a novice. Incidentally, if I re-code the example, should I alter it to Python3 syntax while I'm at it? Is there any good reason to move away from Python2? Kind regards,Barry. -- Barry Drake is a member of the the Ubuntu Advertising team. http://ubuntu.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor