Re: [Tutor] Help with converting a string into a integer
On 01/11/2013 02:36, bob gailer wrote: On 10/31/2013 2:51 PM, Carmen Salcedo wrote: Thanks Bob! :) I'm very new at programming in Python. I appreciate your feedback. Here are some improvements to consider: import string def main(): d = {"1" : phoneTranslator, "2" : backwardString} # map user selection to corresponding function while True: selection = raw_input("Enter you choice. Enter 1 " + "for Phone Translator or 2 for Backward String.") operation = d.get(selection, None) # retrieve corresponding function or None if operation: # did we get something? operation() # call it break # out of the while loop print "Invalid choice" def phoneTranslator(): trans = string.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "222333444555666888") print "Phone Translator " phoneNumber = raw_input ("Please enter the phone number: ").upper() phoneNumber = phoneNumber.translate(trans) print phoneNumber[:3] + "-" + phoneNumber[3:] def backwardString(): print "not implemented" main() Better yet wrap the call to main() so it's not always called when the module gets imported, plus some error handling wouldn't go amiss. For anyone using Python 3 raw_input becomes input and you don't need the import string, it's simply str.maketrans(...). -- 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
Re: [Tutor] Help with converting a string into a integer
On 10/31/2013 2:51 PM, Carmen Salcedo wrote: Thanks Bob! :) I'm very new at programming in Python. I appreciate your feedback. Here are some improvements to consider: import string def main(): d = {"1" : phoneTranslator, "2" : backwardString} # map user selection to corresponding function while True: selection = raw_input("Enter you choice. Enter 1 " + "for Phone Translator or 2 for Backward String.") operation = d.get(selection, None) # retrieve corresponding function or None if operation: # did we get something? operation() # call it break # out of the while loop print "Invalid choice" def phoneTranslator(): trans = string.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "222333444555666888") print "Phone Translator " phoneNumber = raw_input ("Please enter the phone number: ").upper() phoneNumber = phoneNumber.translate(trans) print phoneNumber[:3] + "-" + phoneNumber[3:] def backwardString(): print "not implemented" main() -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
Thanks Bob! :) I'm very new at programming in Python. I appreciate your feedback. Have a great week! Sent from my iPhone On Oct 31, 2013, at 1:07 PM, "bob gailer" wrote: > On 10/31/2013 10:11 AM, Carmen Salcedo wrote: >> Thanks Bob! :) A list is great idea. I'm just trying to figure out how to >> print the number across like a phone number 555- instead of downward. >> I'm stuck on that. > > I repeat what I said before: >> >> There are many ways to get the desired output. >> One is: >> collect the characters in one list, say numberList >> Then use slicing to insert the "-" e.g. numberList[3:3] = "-" >> Then print "".join(numberList) > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
Hi Mark, Thanks for the feedback. I figured it out. Yes, I'm using python 2.7 (typo in the last email). Have a great day. Sent from my iPhone On Oct 31, 2013, at 10:44 AM, "Mark Lawrence" wrote: > On 31/10/2013 02:00, Carmen Salcedo wrote: >> Hi Everyone, >> >> I hope you're having a great week. I'm working on this program that >> converts strings to integers. Can someone please help me out? :) Below >> is the program: >> >> def main(): >> selection = input("Enter you choice. Enter 1 " + >>"for Phone Translator or 2 for Backward String.") >> while selection != 1 and selection !=2: >> print "Invalid choice" > > I can tell from the print that you're using Python 2.x. For reasons that I > won't go into here never, ever use input, use raw_input instead. So try that > and when you hit problems please get back to us, giving the data that Bob > Gailer asked for earlier. > > -- > 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
Thanks Bob! :) A list is great idea. I'm just trying to figure out how to print the number across like a phone number 555- instead of downward. I'm stuck on that. 5 5 5 Thanks again! Carmen Sent from my iPhone On Oct 31, 2013, at 9:02 AM, "bob gailer" wrote: > On 10/31/2013 7:52 AM, Carmen Salcedo wrote: >> I'm not able to post it right now. All I did to the previous program i >> emailed was changed isalpha() to str.isalpha. > That does agree with what you posted or got. > > The part of your original program that should print a character already is > print (str.isalpha() > which raises this exception: > > File "N:\Script2.py", line 37, in phoneTranslator > print(str.isalpha()) > TypeError: descriptor 'isalpha' of 'str' object needs an argument > > it should be > > print n > > Exactly what did you type in response to > phoneNumber = raw_input ("Please enter the phone number: ") > > Note you are not consistent in converting letters: > elif n == "G" or n == "H" or n == "I": > n = "4" > elif n == "J" or n == "K" or n == "L": > n = 5 > all the numbers should be characters e.g. > n = "5" > etc. > > There are many ways to get the desired output. > One is: > collect the characters in one list, say numberList > Then use slicing to insert the "-" e.g. numberList[3:3] = "-" > Then print "".join(numberList) > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
Hi, I'm using python 2.7. I'm editing the program with idle. I use windows 8. I finally got the string to convert to integers, however I can't figure out how to print them in this phone number format 555-5678. The numbers are printing out this way. 5 5 5 5 6 Thank you very much. :) Carmen Sent from my iPhone Sent from my iPhone On Oct 31, 2013, at 6:33 AM, "bob gailer" wrote: > On 10/30/2013 10:00 PM, Carmen Salcedo wrote: > > > Hi Everyone, > > hi > > some guidelines for this list. > post in plain text not html. > tell us what version of Python you are using, what OS, what you use to edit > and run the program. > when replying: >reply-all so a copy goes to the list >put your responses following the relevant text >delete irrelevant text >if you get an exception (error) post the entire traceback. example: > > File "N:\foo.py", line 2, in >2/0 > ZeroDivisionError: division by zero > > > I hope you're having a great week. I'm working on this program that > > converts strings to integers. Can someone please help me out? :) > > What kind of help do you want? > It is a lot easier for us and more profitable for you when you > are specific > tell us what kind of help you need > did you run the program? > what did you get and what did you expect? > > >Below is the program: > > > def main(): > > selection = input("Enter you choice. Enter 1 " + > >"for Phone Translator or 2 for Backward String.") > > while selection != 1 and selection !=2: > > print "Invalid choice" > > selection = input("Please enter you selection. Enter 1 " + > >"for Phone Translator or 2 for Backward String.") > > if selection == 1: > > phoneTranslator() > > elif selection == 2: > > backwardString() > > def phoneTranslator(): > > print "Phone Translator " > > phoneNumber = raw_input ("Please enter the phone number: ") > > phoneNumber = phoneNumber.upper() > > for n in phoneNumber: > > if str.isalpha(n): > > if n == "A" or n == "B" or n == "C": > > n = "2" > > elif n == "D" or n == "E" or n == "F": > > n = "3" > > elif n == "G" or n == "H" or n == "I": > > n = "4" > > elif n == "J" or n == "K" or n == "L": > > n = 5 > > elif n == "M" or n == "N" or n == "O": > > n = 6 > > elif n == "P" or n == "Q" or n == "R" or n == "S": > > n = 7 > > elif n == "T" or n == "U" or n == "V": > > n = "8" > > else: > > n = "9" > > print str.isalpha() > > main() > > Thanks!! :) > > Carmen > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
I'm not able to post it right now. All I did to the previous program i emailed was changed isalpha() to str.isalpha. Thanks Sent from my iPhone On Oct 31, 2013, at 7:09 AM, "bob gailer" wrote: > On 10/31/2013 6:49 AM, Carmen Salcedo wrote: >> Hi, >> >> I'm using python 2.7. I'm editing the program with idle. I use windows 8. >> >> I finally got the string to convert to integers, however I can't figure out >> how to print them in this phone number format 555-5678. >> >> The numbers are printing out this way. >> 5 >> 5 >> 5 >> 5 >> 6 > Please post the new program. > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Geometric sequence
As an aside: It shouldn't be too bad to write a "generator" for the geometric series, so that we can pick out the terms on-demand. # >>> def geometric(base): ... x = 1 ... while True: ... yield x ... x *= base ... >>> twos = geometric(2) # 'twos' is a representation of the geometric series; we can then pick out the elements one at a time: # >>> twos.next() 1 >>> twos.next() 2 >>> twos.next() 4 >>> twos.next() 8 >>> twos.next() 16 >>> twos.next() 32 >>> twos.next() 64 # ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary from a text file
> > > Note: in* 'call' : Update* ,Update it is a function defined in my python > script. My dictionary is too large so i taught rather than using directly > in python program I save it in a text file and when needed i assign it to > dictionary object . How can i assign this text file to dictionary object > and call it? > > To introduce some terms: you are "serializing" some data --- your dictionary --- to disk and back. There are a few libraries that help you do the hard work of translating the nested dictionary structure to some flat string; in addition, they almost always includes a parser to go back from that flat string back to the nested dictionary structure. The 'json' library that Petter Otten suggests is one of these serialization libraries. Try looking into that and see if you can just take advantage of it. Are there other consumers for this data besides just your program? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
On 10/31/2013 10:11 AM, Carmen Salcedo wrote: I'm just trying to figure out how to print the number across like a phone number 555- instead of downward. I'm stuck on that. On further thought: print "%s%s%s-%s%s%s%s" % tuple(numberList) The % operator does formatting. Each %s is replaced by the next string in the tuple. Just be sure you have exactly 7 characters in the list or this will fail. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
On 10/31/2013 10:11 AM, Carmen Salcedo wrote: Thanks Bob! :) A list is great idea. I'm just trying to figure out how to print the number across like a phone number 555- instead of downward. I'm stuck on that. I repeat what I said before: There are many ways to get the desired output. One is: collect the characters in one list, say numberList Then use slicing to insert the "-" e.g. numberList[3:3] = "-" Then print "".join(numberList) -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
On 31/10/2013 02:00, Carmen Salcedo wrote: Hi Everyone, I hope you're having a great week. I'm working on this program that converts strings to integers. Can someone please help me out? :) Below is the program: def main(): selection = input("Enter you choice. Enter 1 " + "for Phone Translator or 2 for Backward String.") while selection != 1 and selection !=2: print "Invalid choice" I can tell from the print that you're using Python 2.x. For reasons that I won't go into here never, ever use input, use raw_input instead. So try that and when you hit problems please get back to us, giving the data that Bob Gailer asked for earlier. -- 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
Re: [Tutor] Help with converting a string into a integer
On 10/31/2013 7:52 AM, Carmen Salcedo wrote: I'm not able to post it right now. All I did to the previous program i emailed was changed isalpha() to str.isalpha. That does agree with what you posted or got. The part of your original program that should print a character already is print (str.isalpha() which raises this exception: File "N:\Script2.py", line 37, in phoneTranslator print(str.isalpha()) TypeError: descriptor 'isalpha' of 'str' object needs an argument it should be print n Exactly what did you type in response to phoneNumber = raw_input ("Please enter the phone number: ") Note you are not consistent in converting letters: elif n == "G" or n == "H" or n == "I": n = "4" elif n == "J" or n == "K" or n == "L": n = 5 all the numbers should be characters e.g. n = "5" etc. There are many ways to get the desired output. One is: collect the characters in one list, say numberList Then use slicing to insert the "-" e.g. numberList[3:3] = "-" Then print "".join(numberList) -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
On 10/31/2013 6:49 AM, Carmen Salcedo wrote: Hi, I'm using python 2.7. I'm editing the program with idle. I use windows 8. I finally got the string to convert to integers, however I can't figure out how to print them in this phone number format555-5678 . The numbers are printing out this way. 5 5 5 5 6 Please post the new program. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary from a text file
On 10/31/2013 2:16 AM, Nitish Kunder wrote: I have a dictionary which is in this format for ex: { '5x' : { '50' : { 'update' : { 'update-from-esxi5.0-5.0_update01' : { 'call' : Update, 'name' : 'Update50u1', 'release' : '15/03/12' }, 'update-from-esxi5.0-5.0_update02' : { 'call' : Update, 'name' : 'Update50u2', 'release' : '21/12/12' }, }, 'patch' : { 'ESXi500-201109001' : { 'call' : Patch, 'name' :'Patch_501', 'release' : '13/09/11' }, 'ESXi500-20001' : { 'call' : Patch, 'name' :'ExpressPatch501', 'release' : '13/09/11' }, }, }, [snip] Does it have to be in a dictionary format? I'd rather use sqlite to store the data. Also please give us a use case. There is probably a much simper and more elegant solution. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with converting a string into a integer
On 10/30/2013 10:00 PM, Carmen Salcedo wrote: > Hi Everyone, hi some guidelines for this list. post in plain text not html. tell us what version of Python you are using, what OS, what you use to edit and run the program. when replying: reply-all so a copy goes to the list put your responses following the relevant text delete irrelevant text if you get an exception (error) post the entire traceback. example: File "N:\foo.py", line 2, in 2/0 ZeroDivisionError: division by zero > I hope you're having a great week. I'm working on this program that converts strings to integers. Can someone please help me out? :) What kind of help do you want? It is a lot easier for us and more profitable for you when you are specific tell us what kind of help you need did you run the program? what did you get and what did you expect? >Below is the program: > def main(): > selection = input("Enter you choice. Enter 1 " + >"for Phone Translator or 2 for Backward String.") > while selection != 1 and selection !=2: > print "Invalid choice" > selection = input("Please enter you selection. Enter 1 " + >"for Phone Translator or 2 for Backward String.") > if selection == 1: > phoneTranslator() > elif selection == 2: > backwardString() > def phoneTranslator(): > print "Phone Translator " > phoneNumber = raw_input ("Please enter the phone number: ") > phoneNumber = phoneNumber.upper() > for n in phoneNumber: > if str.isalpha(n): > if n == "A" or n == "B" or n == "C": > n = "2" > elif n == "D" or n == "E" or n == "F": > n = "3" > elif n == "G" or n == "H" or n == "I": > n = "4" > elif n == "J" or n == "K" or n == "L": > n = 5 > elif n == "M" or n == "N" or n == "O": > n = 6 > elif n == "P" or n == "Q" or n == "R" or n == "S": > n = 7 > elif n == "T" or n == "U" or n == "V": > n = "8" > else: > n = "9" > print str.isalpha() > main() > Thanks!! :) > Carmen -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary from a text file
Nitish Kunder wrote: > I have a dictionary which is in this format > for ex: > > { > '5x' : { > '50' : { > 'update' : { > 'update-from-esxi5.0-5.0_update01' : { > 'call' : Update, > 'name' : 'Update50u1', > 'release' : '15/03/12' > }, > 'update-from-esxi5.0-5.0_update02' : { > 'call' : Update, > 'name' : 'Update50u2', > 'release' : '21/12/12' > }, > }, > 'patch' : { > 'ESXi500-201109001' : { > 'call' : Patch, > 'name' :'Patch_501', > 'release' : '13/09/11' > }, > 'ESXi500-20001' : { > 'call' : Patch, > 'name' :'ExpressPatch501', > 'release' : '13/09/11' > }, > }, > }, > '51' : { > 'update' : { > 'update-from-esxi5.1-5.1_update01' : { > 'call' : Update, > 'name' : 'Update51u1', > 'release' : '25/04/13' > }, > }, > 'patch' : { > 'ESXi510-201210001' : { > 'call' : Patch, > 'name' :'ExpressPatch511', > 'release' : '29/08/13' > }, > 'ESXi510-201212001' : { > 'call' : Patch, > 'name' :'Patch_511', > 'release' : '20/12/12' > }, > }, > }, > }, > } > > Note: in* 'call' : Update* ,Update it is a function defined in my python > script. My dictionary is too large so i taught rather than using directly > in python program I save it in a text file and when needed i assign it to > dictionary object . How can i assign this text file to dictionary object > and call it? You could modify the dict a bit to comply with the json format and then load and post-process it to replace names with functions: import json def Update(): pass def Patch(): pass def post_process(x, names): for k, v in x.items(): if k == u"call": x[k] = names[v] elif isinstance(v, dict): post_process(v, names) with open("tmp.json") as instream: d = json.load(instream) post_process(d, names={u"Patch": Patch, u"Update": Update}) Another option is to use a function similar to ast.literal_eval() that can do name lookups. Here's my adaptation of literal_eval(): import ast def Update(): pass def Patch(): pass def safe_eval(s, names={}): node = ast.parse(s, mode="eval") if isinstance(node, ast.Expression): node = node.body def convert(node): if isinstance(node, ast.Str): return node.s elif isinstance(node, ast.Dict): return {convert(k): convert(v) for k, v in zip(node.keys, node.values)} elif isinstance(node, ast.Name): try: return names[node.id] except KeyError: raise ValueError("Unresolved name {!r}".format(node.id)) raise ValueError("Malformed node or string {!r}".format(node)) return convert(node) with open("tmp.txt") as instream: data = instream.read() d = safe_eval(data, dict(Update=Update, Patch=Patch)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Dictionary from a text file
I have a dictionary which is in this format for ex: { '5x' : { '50' : { 'update' : { 'update-from-esxi5.0-5.0_update01' : { 'call' : Update, 'name' : 'Update50u1', 'release' : '15/03/12' }, 'update-from-esxi5.0-5.0_update02' : { 'call' : Update, 'name' : 'Update50u2', 'release' : '21/12/12' }, }, 'patch' : { 'ESXi500-201109001' : { 'call' : Patch, 'name' :'Patch_501', 'release' : '13/09/11' }, 'ESXi500-20001' : { 'call' : Patch, 'name' :'ExpressPatch501', 'release' : '13/09/11' }, }, }, '51' : { 'update' : { 'update-from-esxi5.1-5.1_update01' : { 'call' : Update, 'name' : 'Update51u1', 'release' : '25/04/13' }, }, 'patch' : { 'ESXi510-201210001' : { 'call' : Patch, 'name' :'ExpressPatch511', 'release' : '29/08/13' }, 'ESXi510-201212001' : { 'call' : Patch, 'name' :'Patch_511', 'release' : '20/12/12' }, }, }, }, } Note: in* 'call' : Update* ,Update it is a function defined in my python script. My dictionary is too large so i taught rather than using directly in python program I save it in a text file and when needed i assign it to dictionary object . How can i assign this text file to dictionary object and call it? Thanks for you help ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with converting a string into a integer
Hi Everyone, I hope you're having a great week. I'm working on this program that converts strings to integers. Can someone please help me out? :) Below is the program: def main():selection = input("Enter you choice. Enter 1 " + "for Phone Translator or 2 for Backward String.")while selection != 1 and selection !=2:print "Invalid choice"selection = input("Please enter you selection. Enter 1 " + "for Phone Translator or 2 for Backward String.")if selection == 1:phoneTranslator()elif selection == 2:backwardString() def phoneTranslator():print "Phone Translator "phoneNumber = raw_input ("Please enter the phone number: ")phoneNumber = phoneNumber.upper() for n in phoneNumber:if str.isalpha(n):if n == "A" or n == "B" or n == "C":n = "2"elif n == "D" or n == "E" or n == "F":n = "3"elif n == "G" or n == "H" or n == "I":n = "4"elif n == "J" or n == "K" or n == "L":n = 5elif n == "M" or n == "N" or n == "O": n = 6elif n == "P" or n == "Q" or n == "R" or n == "S": n = 7elif n == "T" or n == "U" or n == "V": n = "8"else:n = "9" print str.isalpha() main() Thanks!! :) Carmen___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Geometric sequence
On 31.10.2013 04:00, bob gailer wrote: On 10/30/2013 1:08 PM, Peter O'Doherty wrote: Hi List, I know a geometric sequence can be produced by: series = [2**x for x in range(7)] But I would like to curtail the sequence before the last element excedes a certain value. import itertools series = [2**x for x in itertools.takewhile(lambda x: 2**x<60, range(7))] If you first produce an infinite geometric series and take only the elements up to a certain limit you avoid calculating 2**x twice: >>> import itertools as it >>> [x for x in it.takewhile(lambda x: x < 60, (2**x for x in it.count(0)))] >>> [1, 2, 4, 8, 16, 32] Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor