Re: [Tutor] Tutor Digest, Vol 117, Issue 33[ Re: Ideas ? ]
Need a little help with finding a process for this: when a string of text is input, for example: abc def. I want to have each letter shift to the right one place in the alphabet. Thus.. abc def would be output as bcd efg. Any ideas on how to do this? - if you do not want to use the translate: >>> s='abcdef ghijk lmn' >>> k='' >>> for char in s: if char!=' ': k+=str(chr(ord(char)+1)) else: k+=' ' >>> k 'bcdefg hijkl mno' -- If you want to use translate: >>> from string import maketrans >>> your_alphabets = 'abcdefghijklmnopurstuvwzyz' >>> my_alphabets='bcdefghijklmnopqystuvwxyza' >>> s='Let us Test Your language !' >>> s.translate(maketrans(your_alphabets, my_alphabets)) 'Lfu vt Tftu Ypvs mbohvbhf !' Note: my_alphabets to fine tune __ On Mon, Nov 18, 2013 at 6:00 AM, wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > > You can reach the person managing the list at > tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > >1. ideas? (Byron Ruffin) >2. Re: Phython List values :p: (Paradox) > > > -- > > Message: 1 > Date: Sun, 17 Nov 2013 21:27:56 -0600 > From: Byron Ruffin > To: tutor@python.org > Subject: [Tutor] ideas? > Message-ID: > kojukeyg+c...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Need a little help with finding a process for this: > > when a string of text is input, for example: abc def. > I want to have each letter shift to the right one place in the alphabet. > Thus.. > abc def would be output as bcd efg. > > Any ideas on how to do this? > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20131117/56728ee8/attachment-0001.html > > > > -- > > Message: 2 > Date: Mon, 18 Nov 2013 05:49:06 -0500 > From: Paradox > To: tutor@python.org > Subject: Re: [Tutor] Phython List values :p: > Message-ID: <5289f0a2.3010...@pobox.com> > Content-Type: text/plain; charset=windows-1252; format=flowed > > Ayo, > > On 11/18/2013 01:57 AM, Ayo Rotibi wrote: > > > > > > I read that an assignment with an = on lists does not make a copy. > > Instead, assignment makes the two variables point to the one list in > > memory. For instance, if a = [1, 2, 3] and b=a, then b = [1, 2, 3]. > > > > However, I discovered that if I change the value in ?a?, ?b? does not > > take the new value. I thought since it is pointing to the same > > storage as ?a?, ?b? should take the new value? > > > > > It is not clear to me what code you are running, maybe you could give us > the specifics? When I run an assignment like you seem to be talking > about I get the expected result (using ipython for an interactive > session to demonstrate): > > In [1]: a=[1,2,3] > > In [2]: b=a > > In [3]: b > Out[3]: [1, 2, 3] > > In [4]: a[1]=0 > > In [5]: a > Out[5]: [1, 0, 3] > > In [6]: b > Out[6]: [1, 0, 3] > > Is that not what you are seeing? If not perhaps you could show us how > you are doing the list assignment and changing the list a, what output > you are expecting and what output you are getting. > > thomas > > > -- > > Subject: Digest Footer > > ___ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > > > -- > > End of Tutor Digest, Vol 117, Issue 33 > ** > -- Warm regards. Satheesan Varier 860 (970) 2732 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 117, Issue 33[ Re: Ideas ? ]
On 19/11/2013 02:02, Satheesan Varier wrote: If you want help would you please start a new thread and not send a reply to the entire tutor digest, albeit a small one in this instance. Or are you replying to someone else, it's very difficult to see from the mish mash that's arrived at my email client? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Unit testing individual modules
Python 2.4 self-study with Python Programming: An Introduction to Computer Science by Zelle I am trying to import a program that has a conditional execution statement at the end so I can troubleshoot individual modules in the main() program. Below is the textbook example program. When I try to import the program from Idle, I get Traceback (most recent call last): File "", line 1, in -toplevel- import rball ImportError: No module named rball (Also if I try to import with .py or .pyc) From the commandline interface, I can import the program, but __name__ is still "__main__" and I haven't successfully unit tested any of the individual functions. # rball.py # textbook example of top-down design for Monte Carlo raquetball simulation from random import random def main(): #printIntro() #probA, probB, n = getInputs() #winsA, winsB = simNGames(n,probA,probB) #printSummary(winsA, winsB) def printIntro(): print "This program simulates a game of racquetball between two" print 'players called "A" and "B." The ability of each player is' print "indicated by a probability (a number between 0 and 1) that" print "the player wins the point when serving. Player A always" print "has the first serve." def getInputs(): #Returns the three simulation parameters n = input("How many games to simulate? ") a = input("What is the probability player A wins service? ") b = input("What is the probability player B wins service? ") return a, b, n def simNGames(n, probA, probB): #simulates n games of racquetball between players whose # abilities are represented by the prob. of winning serivce #Returns total number of wins for A and B winsA = winsB = 0 for i in range(n): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA = winsA + 1 else: winsB = winsB + 1 return winsA, winsB def simOneGame(probA, probB): #Simulates a single game between players whose # abilities are represented by the prob. of winning service. # Returns final scores for A and B, one game serving = 'A' scoreA = scoreB = 0 while not gameOver(scoreA, scoreB): if serving == 'A': if random()< probA: scoreA = scoreA + 1 else: serving = 'B' else: if random() < probB: scoreB = scoreB + 1 else: serving = 'A' return scoreA, scoreB def gameOver(a,b): # a and b represent running scores for one racquetball game # Returns True if game over, False otherwise return a==15 or b==15 def printSummary(winsA, winsB): # Prints a summary of wins for each player. n = winsA + winsB print print "Games simulated:", n print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100) print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100) printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n,probA,probB) printSummary(winsA, winsB) if __name__ == "__main__": main()___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing individual modules
On 19/11/13 16:06, Patti Scott wrote: Python 2.4 self-study with Python Programming: An Introduction to Computer Science by Zelle I am trying to import a program that has a conditional execution statement at the end so I can troubleshoot individual modules in the main() program. I'm not quite sure what you are doing here. Can you give a bit more detail of how you are using this? How do you import it? Traceback (most recent call last): File "", line 1, in -toplevel- import rball ImportError: No module named rball Is rball in the import path? (sys.path) (Also if I try to import with .py or .pyc) Never include the .py or .pyc in an import From the commandline interface, I can import the program, but __name__ is still "__main__" and I haven't successfully unit tested any of the individual functions. This bit confuses me. Which command line do you mean? The OS or Python interpreter? Also the only way you can test the functions is if name is main. That's because the functions are all defined inside main(). That's a highly unusual pattern... # rball.py # textbook example of top-down design for Monte Carlo raquetball simulation from random import random This import is the only thing that happens if name is not main... def main(): def printIntro():... def getInputs():... def simNGames(n, probA, probB):... def simOneGame(probA, probB):... def gameOver(a,b):... def printSummary(winsA, winsB):... > ... > if __name__ == "__main__": main() Are you really sure that's what you want? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor