Re: [Tutor] Help Needed
I just stumbled on this Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s="hello world" >>> s=s[::-1] >>> print s dlrow olleh >>> --- Gil Cosson Bremerton, Washington 360 620 0431 --- On Mon, 6/15/09, Wayne wrote: From: Wayne Subject: Re: [Tutor] Help Needed To: "Raj Medhekar" Cc: "Python Tutor" Date: Monday, June 15, 2009, 5:28 PM On Mon, Jun 15, 2009 at 3:00 PM, Raj Medhekar wrote: I am looking to build a program that gets a message from the user and then prints it backward. I 've been at it for hours now but I can't seem to figure it out. I've been having trouble trying to index the message so I can print it out backwards. I would've posted my code but I really haven't gotten past the 'raw_input("Enter a message: ")' command line. Any help is gladly appreciated. Thanks! Python treats a string like a list/array. In [1]: mystr = "I'm not a witch!" In [2]: mystr[0] Out[2]: 'I' Same goes for one that's defined with raw_input: In [4]: mystr = raw_input("She turned you into a newt? ") She turned you into a newt? I got better! In [5]: mystr[0] + mystr[1:] Out[5]: 'I got better!' Python has a slice operator the colon, so mystr[0] gives me the character at 0, and mystr[1:] gives me everything from the first character to the end (if you add something after the colon it will only return whatever is up to that point: In [10]: mystr[2:5] Out[10]: 'got' If you know what a loop is, you should be able to figure out how to get the result you're looking for. HTH, Wayne -Inline Attachment Follows- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
how to link two files in python Bollywood news, movie reviews, film trailers and more! Go to http://in.movies.yahoo.com/___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
Hi, This is how I would do it, although there might be more readable solutions: s = raw_input("Enter a message: ") print "".join([s[-letter] for letter in range(len(s)+1)]) Cheers!! Albert-Jan --- On Tue, 6/16/09, Alan Gauld wrote: > From: Alan Gauld > Subject: Re: [Tutor] Help Needed > To: tutor@python.org > Date: Tuesday, June 16, 2009, 1:57 AM > > "Raj Medhekar" > wrote > > > I am looking to build a program that gets a message > from the user and then prints it backward. I 've been at it > for hours now but I can't seem to figure it out. > > So tell us what you re thinking? How would you solve it > manually? > > > I've been having trouble trying to index the message > so I can print it out backwards. > > You can index the characters of a string in Python exactly > as you do a list. > > > I would've posted my code but I really haven't gotten > past the 'raw_input("Enter a message: ")' command > line. > > Its always better to try something even if its wrong, it > lets us see where your thinking is going adrift. Or at least > describe what you'd like to do conceptually. > > Alan G. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Tue, Jun 16, 2009 at 6:19 AM, Febin Ameer Ahsen wrote: > how to link two files in python A few tips on getting good replies: 1) Start with a descriptive subject, such as "importing a file" or "appending one file to another" 2) Actually ask a question and describe what you're trying to do. Without a question mark '?' all you are doing is making a statement. Even if you're asking a question in a non-native language, you should still be able to use somewhat proper punctuation! When you describe what you're trying to do, try to be as accurate as possible. When you say "link two files", that's very open ended. What type of files are you trying to link? How are you trying to link them? Do you have two text files that need to be embedded somehow? Do you have two python files, and you want to include the code from one in the other? Do you have an audio file you want to link with a video file? If you can give us a good idea about what you want, you will likely get a helpful response. -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] which is better solution of the question
*Question :* The first list contains some items, and the second list contains their value (higher is better). items = [apple, car, town, phone] values = [5, 2, 7, 1] Show how to sort the 'items' list based on the 'values' list so that you end up with the following two lists: items = [town, apple, car, phone] values = [7, 5, 2, 1] *Ans. 1* values, items = list(zip(*sorted(zip(values,items), reverse=True))) *Ans. 2* new_values = sorted(values, reverse=True) new_items = [items[x] for x in map(values.index,new_values)] I would like to know which method is better and why? -- With Regards, Abhishek Tiwari ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 64, Issue 80
Wayne already explained slicing but I would like to point out the digit after the second colon changes the default step of the slice. Usually it defaults to 1, here because no values were given it takes the entire string and steps backward. You could set it to 2. First digit, beginning of slice, second digit end of slice, third digit is the jump to the next item in the sequence. HTH, Ben S. > Message: 2 > Date: Mon, 15 Jun 2009 23:53:03 -0700 (PDT) > From: Gil Cosson > To: Raj Medhekar , Wayne > Cc: Python Tutor > Subject: Re: [Tutor] Help Needed > Message-ID: <689147.52804...@web35602.mail.mud.yahoo.com> > Content-Type: text/plain; charset="iso-8859-1" > > I just stumbled on this > > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] > on > win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> s="hello world" > >>> s=s[::-1] > >>> print s > dlrow olleh > >>> > > --- > > Gil Cosson > > Bremerton, Washington > > 360 620 0431 > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which is better solution of the question
On Tue, Jun 16, 2009 at 8:52 AM, Abhishek Tiwari wrote: > *Question :* > The first list contains some items, and the second list contains their > value (higher is better). > > items = [apple, car, town, phone] > values = [5, 2, 7, 1] > > Show how to sort the 'items' list based on the 'values' list so that you > end up with the following two lists: > > items = [town, apple, car, phone] > values = [7, 5, 2, 1] > > *Ans. 1* > > values, items = list(zip(*sorted(zip(values,items), reverse=True))) > > *Ans. 2* > new_values = sorted(values, reverse=True) > new_items = [items[x] for x in map(values.index,new_values)] > > I would use a dict to store the values: {1:'phone', 5:'apple', 2:'car', 7:'town'} - then you just use sorted(mydict.keys(), reverse=True) to access them in a big-endian (most important) first, or without reverse if I wanted the least important value. This assumes that you can't have two values of the same weight, of course. If you're looking for speed, I'd look at timeit: http://docs.python.org/library/timeit.html I don't know which is considered more pythonic, though, so I'm afraid I can't be of much more help than that. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which is better solution of the question
"Abhishek Tiwari" wrote *Ans. 1* values, items = list(zip(*sorted(zip(values,items), reverse=True))) Personally I find that just a bit too dense so I'd break it out to: s = sorted(zip(values,items), reverse=True) values = [v for v,i in s] items = [i for v,i in s] *Ans. 2* new_values = sorted(values, reverse=True) new_items = [items[x] for x in map(values.index,new_values)] I prefer this to the one-liner but don't like the map inside the comprehension. But as the old Irish saying goes: "If I was going there I wouldn't start from here!" From a purely readability point of view, and if I had any control over the data formats I'd personally convert it to a dictionary of value,item pairs and not bother with two lists. Sorting then becomes a unified operation. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which is better solution of the question
On Tue, Jun 16, 2009 at 9:52 AM, Abhishek Tiwari wrote: > Question : > The first list contains some items, and the second list contains their value > (higher is better). > > items = [apple, car, town, phone] > values = [5, 2, 7, 1] > > Show how to sort the 'items' list based on the 'values' list so that you end > up with the following two lists: > > items = [town, apple, car, phone] > values = [7, 5, 2, 1] > > Ans. 1 > > values, items = list(zip(*sorted(zip(values,items), reverse=True))) I like this one though the list() is not needed. > Ans. 2 > new_values = sorted(values, reverse=True) > new_items = [items[x] for x in map(values.index,new_values)] This will become slow (O(n*n)) if the lists are large because it has to scan the values list for each entry in new_values. If you don't really need the sorted values list, you can use dictionary lookup to get keys: In [5]: ranks = dict(zip(items, values)) In [6]: ranks Out[6]: {'apple': 5, 'car': 2, 'phone': 1, 'town': 7} In [8]: sorted(items, key=ranks.__getitem__, reverse=True) Out[8]: ['town', 'apple', 'car', 'phone'] You could pull the sorted ranks from the dict or just sort values independently: In [9]: new_values = [ ranks[item] for item in _8] In [10]: new_values Out[10]: [7, 5, 2, 1] My guess is that the overhead of creating the dict and doing the lookups will make this slower than your first version but that is just a guess. timeit is your friend if speed is your metric. How do you measure "better"? Speed, clarity, ...? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which is better solution of the question
On 6/16/2009 7:49 AM Kent Johnson said... How do you measure "better"? Speed, clarity, ...? ... or the first method you think of that gives the right result. Where else would you find your keys once you've found them? Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best Python Editor
Eddie wrote: > I downloaded the previous version of PyScripter although couldn't get > it to work and after googling it, I downloaded Python Portable 1.1 > (Python 2.6.1 as most sites/books recommend this and not 3) which has > PySCripter included and this then works fine.Ii also downloaded Komod0 > 5.1 and after messing around with these, I think I prefer PyScripter > and will use that for the mean time. > > I'll also take a look at VIM as being able to use the same program for > PHP/CSS/HTML (and Basic if it supports it) as well as hopefully Python > (I've only just started learning it) would be an advantage. > > Thanks guys > Eddie > You might also check SPE (Stani's Python Editor), it's good. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which is better solution of the question
Abhishek Tiwari wrote: > *Question :* > The first list contains some items, and the second list contains their > value (higher is better). > > items = [apple, car, town, phone] > values = [5, 2, 7, 1] > > Show how to sort the 'items' list based on the 'values' list so that you > end up with the following two lists: > > items = [town, apple, car, phone] > values = [7, 5, 2, 1] > > *Ans. 1* > > values, items = list(zip(*sorted(zip(values,items), reverse=True))) > > *Ans. 2* > new_values = sorted(values, reverse=True) > new_items = [items[x] for x in map(values.index,new_values)] > > > I would like to know which method is better and why? Don't know about being better, but this is another alternative: >>> keygen = iter(values) >>> sorted(items, key=lambda x: next(keygen), reverse=True) I'm thinking that maybe sorted/sort should accept a keylist= argument that takes a list/tuple/iterable of keys, so we can write it like this: >>> sorted(items, keylist=values, reverse=True) what do you think? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Conversion question
Quick question. Say I have a string a="Man" and I want to print the string in base2. Is there a python function like there is in perl to do this? Thanks in advance for any input Sent from my Verizon Wireless BlackBerry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
On Tue, Jun 16, 2009 at 12:46 PM, wrote: > Quick question. Say I have a string a="Man" and I want to print the string > in base2. Is there a python function like there is in perl to do this? > Thanks in advance for any input do you mean like this: In [23]: int('Man', 2) --- ValueErrorTraceback (most recent call last) /home/wayne/ in () ValueError: invalid literal for int() with base 2: 'Man' Or do you mean this? In [24]: mystr = 'some string' In [25]: mystr.encode('hex') Out[25]: '736f6d6520737472696e67' HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks for the reply I would like to print the string in binary Man=01001101011101101110 Sent from my Verizon Wireless BlackBerry -Original Message- From: Wayne Date: Tue, 16 Jun 2009 13:13:58 To: Cc: Subject: Re: [Tutor] Conversion question On Tue, Jun 16, 2009 at 12:46 PM, wrote: > Quick question. Say I have a string a="Man" and I want to print the string > in base2. Is there a python function like there is in perl to do this? > Thanks in advance for any input do you mean like this: In [23]: int('Man', 2) --- ValueErrorTraceback (most recent call last) /home/wayne/ in () ValueError: invalid literal for int() with base 2: 'Man' Or do you mean this? In [24]: mystr = 'some string' In [25]: mystr.encode('hex') Out[25]: '736f6d6520737472696e67' HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
xchime...@gmail.com wrote: > Thanks for the reply I would like to print the string in binary > Man=01001101011101101110 > What's M in binary? Nobody knows... What's M in encoded in 8-bit ASCII string: '0b1001101' Source: bin(ord('M')) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which doesn't support bin. If I upgraded how would I go about converting the entire string to 8-bit ASCII? I appreciate your help. On Tue, Jun 16, 2009 at 3:05 PM, Lie Ryan wrote: > xchime...@gmail.com wrote: > > Thanks for the reply I would like to print the string in binary > > Man=01001101011101101110 > > > > What's M in binary? > Nobody knows... > > What's M in encoded in 8-bit ASCII string: > '0b1001101' > Source: bin(ord('M')) > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
On Tue, Jun 16, 2009 at 2:25 PM, Tom Green wrote: > Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which > doesn't support bin. If I upgraded how would I go about converting the > entire string to 8-bit ASCII? > > I appreciate your help. you write the conversion yourself. 1. # convert a decimal (denary, base 10) integer to a binary string (base 2) 2. # tested with Python24 vegaseat6/1/2005 3. 4. def Denary2Binary(n): 5. '''convert denary integer n to binary string bStr''' 6. bStr = '' 7. if n < 0: raise ValueError, "must be a positive integer" 8. if n == 0: return '0' 9. while n > 0: 10. bStr = str(n % 2) + bStr 11. n = n >> 1 12. return bStr 13. 14. def int2bin(n, count=24): 15. """returns the binary of integer n, using count number of digits""" 16. return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)]) 17. 18. # this test runs when used as a standalone program, but not as an imported module 19. # let's say you save this module as den2bin.py and use it in another program 20. # when you import den2bin the __name__ namespace would now be den2bin and the 21. # test would be ignored 22. if __name__ == '__main__': 23. print Denary2Binary(255) # 24. 25. # convert back to test it 26. print int(Denary2Binary(255), 2) # 255 27. 28. print 29. 30. # this version formats the binary 31. print int2bin(255, 12) # 32. # test it 33. print int("", 2) # 255 34. 35. print 36. 37. # check the exceptions 38. print Denary2Binary(0) 39. print Denary2Binary(-5) # should give a ValueError from http://www.daniweb.com/code/snippet285.html# HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks I just happened to find the site myself. I guess I have to pass each character to the function and build the 8-bit ASCII string or is there a better way to do it? On Tue, Jun 16, 2009 at 3:37 PM, Wayne wrote: > On Tue, Jun 16, 2009 at 2:25 PM, Tom Green wrote: > >> Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which >> doesn't support bin. If I upgraded how would I go about converting the >> entire string to 8-bit ASCII? >> >> I appreciate your help. > > > you write the conversion yourself. anks > > > > >1. # convert a decimal (denary, base 10) integer to a binary string (base > 2) > >2. # tested with Python24 vegaseat6/1/2005 >3. >4. def Denary2Binary(n): > >5. '''convert denary integer n to binary string bStr''' >6. >bStr = '' >7. if n < 0: raise ValueError, "must be a positive integer" > >8. if n == 0: return '0' >9. while n > 0: > >10. bStr = str(n % 2) + bStr >11. n = n >> 1 > >12. return bStr >13. >14. def int2bin(n, count=24): > >15. """returns the binary of integer n, using count number of digits""" >16. return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)]) > >17. >18. # this test runs when used as a standalone program, but not as an > imported module >19. ># let's say you save this module as den2bin.py and use it in another > program >20. # when you import den2bin the __name__ namespace would now be den2bin > and the > >21. # test would be ignored >22. if __name__ == '__main__': >23. print Denary2Binary(255) # >24. > >25. # convert back to test it >26. print int(Denary2Binary(255), 2) # 255 > >27. >28. print >29. >30. # this version formats the binary > >31. print int2bin(255, 12) # > >32. # test it >33. print int("", 2) # 255 > >34. >35. print >36. >37. # check the exceptions > >38. print Denary2Binary(0) >39. print Denary2Binary(-5) # should give a ValueError > > > from http://www.daniweb.com/code/snippet285.html# > > HTH, > Wayne > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Tom Green wrote: > Correct 8-bit ASCII. Sorry about that. I am using Python 2.5.2, which > doesn't support bin. If I upgraded how would I go about converting the > entire string to 8-bit ASCII? > AFAIK, earlier versions of python does not have a function/module that converts a number to its binary representation; so you might have to build your own function there. The concept of base-2 conversion is simple, the modulus for powers of 2. >>> def bin(n): ... if n == 0: return '0' ... if n == 1: return '1' ... return mybin(n // 2) + str(n % 2) (that function is simple, but might be slow if you had to concatenate large strings) or generally: def rebase(n, base=2): ''' Convert a positive integer to number string with base `base` ''' if 0 <= n < base: return str(n) return rebase(n // base, base) + str(n % base) than you simply map your string to ord and the bin function, ''.join, and done. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] distutils MANIFEST.in
Hello, a question for people who know how to write MANIFEST.in: How to tell to simply include all files in the package (and subdirs)? I tried: recursive-include *.* ==> warning: sdist: MANIFEST.in, line 1: 'recursive-include' expects ... recursive-include . *.* ==> warning: no files found matching '*.*' under directory '.' recursive-include pijnu *.* (pijnu is the name of the package) ==> warning: no files found matching '*.*' under directory 'pijnu' As a consequence, MANIFEST only includes: README setup.py and install installs nothing else the egg_info file. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to change the working directory in IDLE
Hi Tutors, I"m using Python 2.6.2 and the IDLE tool (also v. 2.6.2). However, when I open the editor I cannot seem to change the directory so as to allow for easy access to my modules. So, for example, the following occurs: >>> os.chdir('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') >>> os.getcwd() '/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests' >>> import CSITest Traceback (most recent call last): File "", line 1, in import CSITest ImportError: No module named CSITest What am I doing wrong? Thanks Elisha ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having with this code is that the the first character of the message that is reversed does not come up. Is there a solution to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use of break? Thanks for the help! Peace, Raj My Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[begin:end:-1] break raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
You are putting far too much work into the solution. Look up slicing on the python web page. Then, as an example, In [1]: s1 = 'hello world' In [2]: s1[::-1] Out[2]: 'dlrow olleh' Hope this helps, Robert On Tue, 2009-06-16 at 14:25 -0700, Raj Medhekar wrote: > I had previously emailed y'all regarding inverting a message input by > the user of the program backwards. After much contemplation on your > earlier replies I came up with the code I have included in this email. > The problem I am having with this code is that the the first character > of the message that is reversed does not come up. Is there a solution > to this? For my message that I input I used "take this" to test it, > use the same message when the program prompts you to enter the message > and run it, you'll see what I mean. Also is there a way to say reverse > the string in a way so the reversed string would result to "this take" > if you use my example? And is there a way to stop the loop without the > use of break? Thanks for the help! > > Peace, > Raj > > My Code: > > # Backward message > # program gets message from user then prints it out backwards > > message = raw_input("Enter your message: ") > > print message > > high = len(message) > low = -len(message) > > begin=None > while begin != "": > begin = int(high) > > if begin: > end = int(low) > > print "Your message backwards is", > print message[begin:end:-1] > break > > > raw_input("\n\nPress the enter key to exit") > > > > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
So I figured out the solution to the missing letter and I will post my code here. But I still need help figuring out the other stuff (please see my original message included in this email)! Thanks for putting up with me. Python is slowly but surely coming to me! I am psyched since this is the first programming language that I am learning! Thanks all for the assistance! -Raj New Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[::-1] break raw_input("\n\nPress the enter key to exit") My Original message: I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having with this code is that the the first character of the message that is reversed does not come up. Is there a solution to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use of break? Thanks for the help! Peace, Raj My Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[begin:end:-1] break raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Raj Medhekar wrote: > So I figured out the solution to the missing letter and I will post my > code here. But I still need help figuring out the other stuff (please > see my original message included in this email)! Thanks for putting up > with me. Python is slowly but surely coming to me! I am psyched since > this is the first programming language that I am learning! Thanks all > for the assistance! > > -Raj > > New Code: > # Backward message > # program gets message from user then prints it out backwards > > message = raw_input("Enter your message: ") > > print message > > high = len(message) > low = -len(message) > > begin=None > while begin != "": > begin = int(high) > > if begin: > end = int(low) > > print "Your message backwards is", > print message[::-1] > break > > > raw_input("\n\nPress the enter key to exit") > All that can be simplified to two lines: message = raw_input("Enter your message: ") print "Your message backwards is", message[::-1] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to change the working directory in IDLE
Elisha Rosensweig wrote: > Hi Tutors, > > I"m using Python 2.6.2 and the IDLE tool (also v. 2.6.2). However, when > I open the editor I cannot seem to change the directory so as to allow > for easy access to my modules. So, for example, the following occurs: > > os.chdir('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') os.getcwd() > '/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests' import CSITest > > Traceback (most recent call last): > File "", line 1, in > import CSITest > ImportError: No module named CSITest > > > What am I doing wrong? http://docs.python.org/tutorial/modules.html#the-module-search-path You probably want to append to sys.path in your script, or to the PYTHONPATH environment variable, instead of using os.chdir. import sys sys.path.append('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') import CSITest HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to change the working directory in IDLE
Martin Walsh wrote: > Elisha Rosensweig wrote: >> Hi Tutors, >> >> I"m using Python 2.6.2 and the IDLE tool (also v. 2.6.2). However, when >> I open the editor I cannot seem to change the directory so as to allow >> for easy access to my modules. So, for example, the following occurs: >> >> os.chdir('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') > os.getcwd() >> '/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests' > import CSITest >> Traceback (most recent call last): >> File "", line 1, in >> import CSITest >> ImportError: No module named CSITest >> >> >> What am I doing wrong? > > http://docs.python.org/tutorial/modules.html#the-module-search-path > > You probably want to append to sys.path in your script, or to the > PYTHONPATH environment variable, instead of using os.chdir. Sorry, 'in your script' should read something like 'in Idle'. Here's another doc reference. http://docs.python.org/tutorial/modules.html#tut-standardmodules > > import sys > sys.path.append('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') > > import CSITest > > HTH, > Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
> My Original message: > > I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much > contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having > with this code is that the the first character of the message that is reversed does not come up. Is there a solution > to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you > to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the > reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use > of break? Thanks for the help! > As to your second part of your request you can use the function split. http://www.python.org/doc/2.4.4/lib/string-methods.html >>> mystring="Mary had a little lamb." >>> mystring.split() ['Mary', 'had', 'a', 'little', 'lamb.'] >>> And then you can use slicing to reverse the created list the same way you did with the full string. Chris___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best Python Editor
On 6/15/2009 12:14 PM Michael Powe said... On Mon, Jun 15, 2009 at 06:34:04AM -0700, Emile van Sebille wrote: I'm wondering if there might be documented benefits to migrating from my horse and buggy. :) Are you in a hurry to get somewhere? ;-) If 20 LOC/day is average nowadays, how fast do you need to be going? I recently worked on a module for a large existing Java application. The module I wrote had to be plugged in to the existing code base. So of course, I had to have all kinds of tie-ins to existing libraries and classes. First, I couldn't run the full application, so I had to rely on unit testing to verify my functionality. Second, I had to connect to hundreds of classes inside the application. I'm not that smart -- I could not have done it without NetBeans, which has fantastic introspection and can tell me most of the ways I'm violating protocol while I'm working. This is a good use case. Unfortunately, I'm still supporting several 30-35 year old 250k line basic applications and associated glue apps that don't play nice with modern tools.And each time I've considered switching (for the glue bits), the time to become productive with a new tool was taking as long as the project estimated time to implement and so it just hasn't happened. I stubbed out a lot of stuff and prototyped in jEdit. But when it was game on, I had to go to NB. It probably comes down to, How much stuff can you carry in your head? several 35 year old 250k line basic applications and associated glue apps :) Also the names of my kids, but not always in the proper one-to-one association. :)) Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing a list to a window
On Tue, Jun 16, 2009 at 4:27 PM, Essah Mitges wrote: > > What I am trying to do is print a high score text file to a pygame window > it kinda works...I don't know how to go about doing this... > Do you know how to print text to a window? to read a file, just in a terminal window: f = open('somefile.txt', 'r') for line in f.readlines(): print line Just translate that. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
"Raj Medhekar" wrote ... I came up with the code I have included in this email. Wy to complicated! message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) you don't need the int calls because lebn returns an int. But you don't even need begin and end since they are just aliases to high and low! And since you never change begin after assigning to high you don't need the loop or the if statement! All thats left is what is below: print "Your message backwards is", print message[high:low:-1] But there is a bug because you tried to be too clever and got confused with the -len() for end. So for 'hello' you are calling print 'hello'[5:-5:-1] but you really needed print 'hello'[-1:-6:-1] Look at the description of slicing to see why... However all you really need to do is print message[::-1] and let Python figure out the boundaries itself. Keep it simple... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing a list to a window
"Essah Mitges" wrote What I am trying to do is print a high score text file to a pygame window it kinda works... How do you define kinda? It doesn't look like it works to me. The function main defined as def main(): high_file = open_file("high_score.txt", "r") score = next_block(high_file) global score high_file.close() score is a local variable and has a value assigned Then you use gloobal which will have no affect so far as I can tell. Finally this function is being replaced by the second function main you defined. You might like to try getting it to work by printing on a console first! Then worry about the GUI bits. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
wrote mystring="Mary had a little lamb." mystring.split() ['Mary', 'had', 'a', 'little', 'lamb.'] And then you can use slicing to reverse the created list the same way you did with the full string. Or use the reverse() method of the list... Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
Raj Medhekar wrote: So I figured out the solution to the missing letter and I will post my code here. But I still need help figuring out the other stuff (please see my original message included in this email)! Thanks for putting up with me. Python is slowly but surely coming to me! I am psyched since this is the first programming language that I am learning! Thanks all for the assistance! -Raj New Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[::-1] break raw_input("\n\nPress the enter key to exit") My Original message: I had previously emailed y'all regarding inverting a message input by the user of the program backwards. After much contemplation on your earlier replies I came up with the code I have included in this email. The problem I am having with this code is that the the first character of the message that is reversed does not come up. Is there a solution to this? For my message that I input I used "take this" to test it, use the same message when the program prompts you to enter the message and run it, you'll see what I mean. Also is there a way to say reverse the string in a way so the reversed string would result to "this take" if you use my example? And is there a way to stop the loop without the use of break? Thanks for the help! Peace, Raj My Code: # Backward message # program gets message from user then prints it out backwards message = raw_input("Enter your message: ") print message high = len(message) low = -len(message) begin=None while begin != "": begin = int(high) if begin: end = int(low) print "Your message backwards is", print message[begin:end:-1] break raw_input("\n\nPress the enter key to exit") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Hello, The following works also. msg = raw_input("\nPlease enter a message to print backwards: ") x = range(0,len(msg)) x.reverse() for i in x: print msg[i], --ayyaz ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help needed
On Tue, Jun 16, 2009 at 8:59 PM, ayyaz wrote: > The following works also. > > msg = raw_input("\nPlease enter a message to print backwards: ") > x = range(0,len(msg)) > x.reverse() > for i in x: print msg[i], or even simpler, allow range to generate the reverse: range(len(msg)-1, -1, -1) Some explanation: len will actually give you a value that is out of range, so you need len-1. So you'll start at the end, and go until you hit -1, and your step is -1. If you did -2 at the end, you'll get every other letter in reverse. And simpler still, just add that (or substitute xrange) to the loop: for i in xrange(len(msg)-1, -1,-1): print msg[i] HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Needed
> Also is there a way to say reverse the string in a way so the reversed > string would result to "this take" if you use my example? And is there a way > to stop the loop without the use of break? Thanks for the help! > Sure. First take your string S and use S.split() to get a list of the individual words in the string. Then you can use similar techniques to those previously proposed for the first problem to reverse the order. Elisha ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor