Re: [Tutor] Unknown Cause of Error
On 11/05/13 07:12, Steven D'Aprano wrote: def menu(): print "Welcome to Tomb Explorer!" print "A game of Exploration from Bulldog Development" print "Press [1] to Play or [2] to Exit" menu1=raw_input(" >> ") if menu1== "2": quit() if menu1== "1": room1() menu() The second problem is that *inside* the "menu" function you make a recursive call to itself. While that is sometimes a legitimate technique, this time it will lead to problems as the menu function gets called again and again and again and again in an (almost) infinite loop. To the OP, not Steven... Beginners often use this technique to deliberately create a loop within their functions. If that is what you wanted then it's better to use one of Pythons native loop structures, 'while' or 'for'. 'for' is generally used when the number of iterations is known or can be calculated. So in your case a 'while' loop is more appropriate. Typically your code should look like: while True: # loop 'forever' # ...display menu if menu1== "2": break # exit the while loop elif menu1== "1": room1() elif... # other menu choices. else: # display invalid choice error HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 111, Issue 24
On 11/05/13 06:17, Alan Gauld wrote: Pascal is still case agnostic and in that community its often seen as a benefit since it avoids a whole class of "error" - when you type the case of a word wrongly... Interesting that you say that. Just the other week I was reading a page somewhere talking about some Pascal compiler, and it made a comment that "by popular request" the next version would allow case sensitive variables. Personally, I don't understand how moderately intelligent English-speaking people can apparently have so much trouble with capitalization. It's very simple: capitalization is the difference between: "I helped my Uncle Jack off a horse." and "I helped my uncle jack off a horse." Case sensitivity really helps when programming too. For example, the usual convention is that classes have an initial capital letter. (Although built-in classes tend to break this convention, mostly for pragmatic reasons.) Instances tend to be lower case. So for example I have code that looks like this: history = History() # actual code, copied from one of my modules and to anyone who understands the convention, it is obvious: I take a History class, create an instance, and call it history. There is no conflict between the two, since they differ in case. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] range efficiency
On 11/05/13 07:19, Jim Mooney wrote: If I'm using a variable-dependent range in a for loop, is Python smart enough to figure the variable once so I don't have to hoist it up? That is for c in range(0,x+2), is x+2 figured once or every time through the loop? I'm assuming it's once but I like to verify. Yes, it is calculated once. Remember that range() is not a syntax feature, it is a function that returns an object. Since it is a function, its argument must be figured *before* the function is called, and once the range function has returned, it is done. So you can write something like this in Python 3: py> x = 3 py> values = range(0, x+2) py> print(values) range(0, 5) py> for c in values: ... print(c) ... 0 1 2 3 4 Python 2 is the same, except that instead of getting that mysterious "range(0, 5)" line printed, you get an actual list of values [0, 1, 2, 3, 4]. By the way, you don't need to include the starting value of 0 for range. range(0, x+2) is exactly the same as range(x+2). Here are a couple of "monkey-patching" tricks for testing things like this. Let's patch the range function to display when it is called, and make a chatty little object that prints when it is added to. === cut === saved_range = range def range(*args): print("calling range function") return saved_range(*args) class Chatty(int): def __add__(self, other): print("adding %s and %s" % (self, other)) return int.__add__(self, other) x = Chatty(3) for c in range(x+2): print(c) === cut === If you copy the code between the "cut" lines, and paste into an interactive interpreter, you will see what Python does when executing this sort of for-loop. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unknown Cause of Error
On 11/05/13 12:13, Jack Little wrote: I have a slight problem. My program will not open. What do you mean "open"? How are you trying to open it? What editor are you opening it in? Or do you mean your program will not *run*? How are you trying to run it? On top of that, I have written similar programs all to no avail. I am creating a text adventure and want there to be different rooms. Here is my code: def menu(): print "Welcome to Tomb Explorer!" print "A game of Exploration from Bulldog Development" print "Press [1] to Play or [2] to Exit" menu1=raw_input(" >> ") if menu1== "2": quit() if menu1== "1": room1() menu() I can see two obvious problems here. First, you define a function, "menu", but do not call it when the program runs. You must call the function, or it will not do anything. The second problem is that *inside* the "menu" function you make a recursive call to itself. While that is sometimes a legitimate technique, this time it will lead to problems as the menu function gets called again and again and again and again in an (almost) infinite loop. Fortunately, you can fix both problems with one edit: *indented* code is part of the function, code that is not indented is not. So: def menu(): print "Welcome to Tomb Explorer!" print "A game of Exploration from Bulldog Development" print "Press [1] to Play or [2] to Exit" menu1=raw_input(" >> ") if menu1== "2": quit() if menu1== "1": room1() menu() -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unknown Cause of Error
On Sat, May 11, 2013 at 12:13 PM, Jack Little wrote: > I have a slight problem. My program will not open. On top of that, I have > written similar programs all to no avail. I am creating a text adventure and > want there to be different rooms. Here is my code: > > > def menu(): > print "Welcome to Tomb Explorer!" > print "A game of Exploration from Bulldog Development" > print "Press [1] to Play or [2] to Exit" > menu1=raw_input(" >> ") > if menu1== "2": > quit() > if menu1== "1": > room1() > menu() You are not calling your function: menu(). Just move the menu() ( your last line) to the left: def menu(): print "Welcome to Tomb Explorer!" print "A game of Exploration from Bulldog Development" print "Press [1] to Play or [2] to Exit" menu1=raw_input(" >> ") if menu1== "2": quit() if menu1== "1": room1() menu() HTH,Amit. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Unknown Cause of Error
I have a slight problem. My program will not open. On top of that, I have written similar programs all to no avail. I am creating a text adventure and want there to be different rooms. Here is my code: def menu(): print "Welcome to Tomb Explorer!" print "A game of Exploration from Bulldog Development" print "Press [1] to Play or [2] to Exit" menu1=raw_input(" >> ") if menu1== "2": quit() if menu1== "1": room1() menu() Thanks A Ton!___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] range efficiency
Jim Mooney wrote: > If I'm using a variable-dependent range in a for loop, is Python smart > enough to figure the variable once so I don't have to hoist it up? > > That is for c in range(0,x+2), is x+2 figured once or every time > through the loop? I'm assuming it's once but I like to verify. > > I'll figure this myself once I get an editor I like with a profiler, > but I'm still trying editors and don't want to spend all my time on > them. So far I'm in between too simple, overkill, or buggy. And I > don't want anything that uses Java. I'm allergic to Java ;') > > -- > Jim Mooney > > "For anything that matters, the timing is never quite right, the > resources are always a little short, and the people who affect the > outcome are always ambivalent." x+2 is only calculated once and then passed to range. The for loop then iterates over the range object. It is not recreating/re-calling range(). Note in python 2 you should use xrange as it is a generator and 1. lazy 2. more memory efficient. Unlike Python 2, in Python 3 range is a generator. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] range efficiency
On 05/10/2013 05:19 PM, Jim Mooney wrote: If I'm using a variable-dependent range in a for loop, is Python smart enough to figure the variable once so I don't have to hoist it up? That is for c in range(0,x+2), is x+2 figured once or every time through the loop? I'm assuming it's once but I like to verify. It's only done once. I'll figure this myself once I get an editor I like with a profiler, but I'm still trying editors and don't want to spend all my time on them. So far I'm in between too simple, overkill, or buggy. And I don't want anything that uses Java. I'm allergic to Java ;') You don't need to use a profiler, or an IDE to answer a question like this. A very useful thing is the dis module: http://pymotw.com/2/dis/index.html http://docs.python.org/library/dis.html#dis.disassemble It shows you the output of the Python compiler. It doesn't take long to learn to read the code it displays. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] range efficiency
If I'm using a variable-dependent range in a for loop, is Python smart enough to figure the variable once so I don't have to hoist it up? That is for c in range(0,x+2), is x+2 figured once or every time through the loop? I'm assuming it's once but I like to verify. I'll figure this myself once I get an editor I like with a profiler, but I'm still trying editors and don't want to spend all my time on them. So far I'm in between too simple, overkill, or buggy. And I don't want anything that uses Java. I'm allergic to Java ;') -- Jim Mooney “For anything that matters, the timing is never quite right, the resources are always a little short, and the people who affect the outcome are always ambivalent.” ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 111, Issue 24
On 10/05/13 20:02, Jim Mooney wrote: As regards camelCaps, all I see in the Py Lib is underlines, so I guess that's more of a convention and I might as well stick to it, Yep. I think both camelCaps and underlines are often artifacts having to use short names years ago. Not so much short names but the fact that white space wasn't allowed and you needed a way to separate words. When space was short - often only 6 significant characters (and in at least one case I've seen, only 4) - it was common to miss out vowels and other such tricks. Although on bigger projects a name index was maintained(*) and all variables had unique numeric IDs prefixing the meaningful name (although only 4 or 6 chars were meaningful often you could have up to 32 or 64 chars in total...). And of course early BASIC just gave you single character variables although you had separate lists for numbers and strings (A and A$ for example). And of course lisp uses hyphens for its names like: make-list (*) And there would be a project name meister to whom you had to apply for a set of numeric prefixes to use on your variables! If you needed to create a new variable it could take a couple of days before you could actually use it in your code! Ah, the joys of big projects! :-) caps for a program name are confusing, anyway. I never figured why Msoft did that. Because DOS came from CP/M and CP/M did that. And CP/M was influenced by the early Dartmouth mainframes which used upper-case only... And on an 8 bit computing platform limiting the number of different names possible was a good thing! Pascal is still case agnostic and in that community its often seen as a benefit since it avoids a whole class of "error" - when you type the case of a word wrongly... Even modern IBM mainframes are still case insensitive - or more specifically they convert it all to upper case. One of the reasons I dislike COBOL programming! Since this is a beginner list, I think bringing up Python Preferred Style is reasonably on topic. For sure, it crops up regularly. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 111, Issue 24
>> BTW, does your "better name" mean that camelCaps are discouraged in >> Python? > > No, although most Python modules are all lowercase. But I really meant that > your module should be for more than just making lists, it should eventually > have all the functions needed to manage the list. > > Eventually maybe even a class to encapsulate those functions > as methods. Good point. But I'm just making a test list for now to feed into a sort program, so I'll gussy it up later. I'm not using sort(), so I can get the practice of raw-coding sorts. Since getting indexes is roundabout in a Py for-loop, I'll have to think about the best algorithm ;') As regards camelCaps, all I see in the Py Lib is underlines, so I guess that's more of a convention and I might as well stick to it, since some programs might become modules. I don't want to spend forever deciding what style to use, when I could be programming. I see there is a proposed manual of style at http://www.python.org/dev/peps/pep-0008/ so I'll look at that. I think both camelCaps and underlines are often artifacts having to use short names years ago. A plain language phrase that isn't hard to spell or almost purposely confusing (like repeated letters on a word boundary) should do most times without camelCaps or underlines, unless you really need them for clarity, so I'll go with underlines, but only when needed for clarity. And of course, a plain, descriptive name makes a program almost self-documenting, so you save on hashmarks. I'm using Windows, which doesn't recognize case difference, so camel caps for a program name are confusing, anyway. I never figured why Msoft did that. Even in normal English, cASe is enfOrcEd for good reason. By trying to make it simpler, Msoft made it harder (Not the first time that's been done ;') Since this is a beginner list, I think bringing up Python Preferred Style is reasonably on topic. I'm a retired webmaster, so readability and idiot-proofing are a BIG item with me. Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger/IDE that can be launched from a remote command line
On Fri, May 10, 2013 at 10:58 AM, Michael O'Leary wrote: > I am working on a project in which the code and data I am working with are > all on an Amazon EC2 machine. So far I have been ssh'ing to the EC2 machine > in two terminal windows, running emacs or vi in one of them to view and > update the code and running the "python -m pdb ..." debugger in the other > one to step through the code. > > I would prefer to work with an IDE that displays and updates program state > automatically, but I don't know which ones I could launch from a remote > machine and have it display within a terminal window or use XWindows or GTK > to display in its own window. Are there any Python debuggers or IDEs that > can be used in this kind of setting? > Thanks, > Mike > > I think IPython could be a useful here. Kick start a IPython notebook on Amazon machine and open it over https locally. More information on this here http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html -Abhi > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python debugger/IDE that can be launched from a remote command line
I am working on a project in which the code and data I am working with are all on an Amazon EC2 machine. So far I have been ssh'ing to the EC2 machine in two terminal windows, running emacs or vi in one of them to view and update the code and running the "python -m pdb ..." debugger in the other one to step through the code. I would prefer to work with an IDE that displays and updates program state automatically, but I don't know which ones I could launch from a remote machine and have it display within a terminal window or use XWindows or GTK to display in its own window. Are there any Python debuggers or IDEs that can be used in this kind of setting? Thanks, Mike ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 111, Issue 24
> As with any other module you need to specify the module when using its > contents: > > newRandomList = makeRandomList.createRandomList() > > BTW. A better name for the module is probably just randomlist > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ Ah, me so stupid. That workede. Although I recall making a very simple test program that didn't use the module prefix and it worked for some odd reason. I just made a dummy prog that said print('thisworks'), put it in Lib, imported with a test prog, and it printed out. Which is why I got confused. Oh well, I'll just ignore that and use the prefix. BTW, does your "better name" mean that camelCaps are discouraged in Python? I loathe finding the underline key and figured it was a good way to always avoid Python reserved words. Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MutableString/Class variables
> From: Steven D'Aprano > To: tutor@python.org > Cc: > Sent: Friday, May 10, 2013 1:28 PM > Subject: Re: [Tutor] MutableString/Class variables > > On 09/05/13 22:10, Albert-Jan Roskam wrote: >> Hello, >> >> I was just playing a bit with Python and I wanted to make a mutable string, > that supports item assignment. Is the way below the way to do this? > > > Guido's time machine strikes again: > > py> from UserString import MutableString > py> s = MutableString("Hello world!") > py> s[-1] = '?' > py> print s > Hello world? > > > You can see the source code here: > > http://hg.python.org/cpython/file/2.7/Lib/UserString.py > > and the docs: > > http://docs.python.org/2/library/userdict.html#module-UserString > > > Note, however, that MutableString is (allegedly) very inefficient, and has > been > removed from Python 3.x. (It seems to me that it cannot possibly be that much > more inefficient than using immutable strings. It's just a wrapper around an > immutable string with a few convenience methods.) > > But really, unless you are dealing with truly humongous strings, you're > better off just sticking to the standard Python built-in string (unicode) > type. > And if by chance you really do need a mutable string-like data structure, you > probably should look at something like ropes, implemented in C or Cython. > > http://en.wikipedia.org/wiki/Rope_(data_structure) Hi Steven, "[...] the purpose of this class is an educational one: to prevent people from inventing their own mutable string class" Whh, twilight zone! ;-))) Still interesting to see how the author implemented __setitem__. In my version, slices were not supported. Interesting to hear about ropes. I did not know about this. Here is another page where a Python implementation of a rope data structure is mentioned: http://kmike.ru/python-data-structures/#ropes Regards, Albert-Jan > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MutableString/Class variables
On 09/05/13 22:10, Albert-Jan Roskam wrote: Hello, I was just playing a bit with Python and I wanted to make a mutable string, that supports item assignment. Is the way below the way to do this? Guido's time machine strikes again: py> from UserString import MutableString py> s = MutableString("Hello world!") py> s[-1] = '?' py> print s Hello world? You can see the source code here: http://hg.python.org/cpython/file/2.7/Lib/UserString.py and the docs: http://docs.python.org/2/library/userdict.html#module-UserString Note, however, that MutableString is (allegedly) very inefficient, and has been removed from Python 3.x. (It seems to me that it cannot possibly be that much more inefficient than using immutable strings. It's just a wrapper around an immutable string with a few convenience methods.) But really, unless you are dealing with truly humongous strings, you're better off just sticking to the standard Python built-in string (unicode) type. And if by chance you really do need a mutable string-like data structure, you probably should look at something like ropes, implemented in C or Cython. http://en.wikipedia.org/wiki/Rope_(data_structure) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyScripter header?
> Sent: Wednesday, May 8, 2013 1:38 PM > Subject: Re: [Tutor] PyScripter header? > > El 08/05/13 05:10, Jim Mooney escribió: >> >> PyScripter has enough extra features, that I'll probably switch to it. >> But has anyone found any odd problems with it? Tks. >> >> Jim >> > > Given that your main() question ;c) has been answered, you might also > want to give Spyder a try before switching. Spyder looks pretty cool. I will also give it a try. Does it have integretion with e,g Git? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MutableString/Class variables
>Subject: Re: [Tutor] MutableString/Class variables >On 05/09/2013 09:16 AM, Albert-Jan Roskam wrote: >>> On 05/09/2013 08:10 AM, Albert-Jan Roskam wrote: Hello, I was just playing a bit with Python and I wanted to make a mutable string, >>> that supports item assignment. Is the way below the way to do this? The part I am not sure about is the class variable. Maybe I should also >>> have reimplemented __init__, then call super inside this and add an updated >>> version of "self" so __repr__ and __str__ return that. More general: >>> when should class variables be used? I am reading about Django nd there >>> they are >>> all over the place, yet I have always had the impression their use is not >>> that >>> common. >>> >>> You use a class attribute (not class variable) whenever you want to have >>> exactly one such value for the entire program. If you want to be able >>> to manipulate more than one, then you use an instance attribute, >>> typically inside the __init__() method. >> >> Hmmm, so is it fair to say that this is the OOP equivalent of 'nonlocal' in >> Python 3? It has meaning inside the entire scope of the class, not outside >> it, there not 'global'. >> >> Here's a modified version; in my previous attempts I messed up the arguments >> of super(). ;-) This feels better. But then again, I never *really* >> understood __new__ entirely. >> >> class MutableStr(str): >> def __init__(self, s): >> super(str, MutableStr).__init__(s) >> self.s = s >> def __repr__(self): >> return self.s >> def __str__(self): >> return self.s >> def __setitem__(self, key, item): >> self.s = self[:key] + item + self[key+1:] >> >> # produce results as intended >> mstr = MutableStr("01234X678") >> mstr[5] = "&" >> print str(mstr) >> print unicode(mstr) >> mstr = MutableStr("01234X678") >> mstr[8] = "&" >> print str(mstr) >> print unicode(mstr) >> >> # output: >> 01234&678 >> 01234&678 >> 01234X67& >> 01234X67& >> >> >> > >That's all well and good as long as you only use those particular >methods. But try: > >print "aaa" + mstr > #The changes you made to mstr have "vanished" > >print(type(mstr)) >mstr += "def" >print(mstr) >print(type(mstr)) > #now it's no longer a MutablsStr Hi Oscar, Dave, Alan, Thanks. I agree that the conclusion is that inheriting from str is not a good idea (unless I'd reimplement each and every method, which would largely make inheritance useless). Using a bytearray as a basis instead is an ingenious alternative. Regards, Albert-Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bad name in module
On 05/10/2013 01:45 AM, Jim Mooney wrote: I have a simple program, below, to create a specified list of random integers, which works fine. I saved it to Lib as makeRandomList.py, then imported it to a sorter.py program, like so. The import doesn't fail: import makeRandomList newRandomList = createRandomList() But I then get the following error: File "c:\Python33\Progs\sorter.py", line 3, in builtins.NameError: name 'createRandomList' is not defined What am I doing wrong in creating the library module below, which works fine as a standalone? The module is fine, but you need to understand better how to reference it when importing. The normal import statement makes a namespace, which contains all the top-level symbols in the module. So import makeRandomList creates a new namespace makeRandomList, which contains in this case one symbol. To call the function, you need to use that namespace: newRandomList = makeRandomList.createRandomList() Alternatively, if there are only a few symbols (one in this case) you need from the imported namespace, you can name them in the other import form: from makeRandomList import createRandomList Now you can just call createRandomList() directly. These are the same rules you use when importing from somebody else's library. So when you want to use the argv symbol from the sys library, you can either do this: import sys and use sys.argv in your code. Or you can use from sys import argv and now you can use argv directly. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor