Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'string': 0.09; 'ascii': 0.09; 'converts': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'throws': 0.09; 'def': 0.12; 'character.': 0.16; 'length.': 0.16; 'ord': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'uppercase': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'header:User-Agent:1': 0.23; 'adds': 0.24; 'define': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'character': 0.29; 'converting': 0.30; 'gives': 0.31; 'factor': 0.31; 'int,': 0.31; 'mod': 0.31; 'subject:numbers': 0.31; 'third': 0.33; 'problem': 0.35; 'test': 0.35; 'functions.': 0.36; 'returning': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'letters': 0.60; 'negative': 0.60; 'first': 0.61; 'back': 0.62; 'email addr:gmail.com': 0.63; 'sum': 0.64; '26,': 0.68; 'containing': 0.69; 'capital': 0.73; 'other.': 0.75; 'how.': 0.84; 'results,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: converting letters to numbers Date: Tue, 8 Oct 2013 21:24:03 +0000 (UTC) References: <26151b64-4f5e-4ee9-81ac-26679932f43d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.36 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381267474 news.xs4all.nl 15982 [2001:888:2000:d::a6]:60650 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56444 On 8/10/2013 10:28, kjakupak@gmail.com wrote: > I have to define a function add(c1, c2), where c1 and c2 are capital letters; the return value should be the sum (obtained by converting the letters to numbers, adding mod 26, then converting back to a capital letter). > > All I have so far is: > > def add(c1, c2): > ord(c1) - ord('a') + 1 > ord(c2) - ord('a') + 1 > > I know I need to use ord and chr, just not sure how. Factor the problem into three functions. one function converts a one-character string into an int, or gives an exception if the character. isn't uppercase ASCII. Second function converts a small int into a string containing one uppercase ASCII letter, throwing an exception if negative or above 25. Third function takes two string arguents, throws an exception if either of them is not exactly one character in length. Then it calls the first function twice, adds the results, modulos it, and calls the second function, returning its return value. Which of these is giving you trouble? Notice you can use the first two functions to test each other. -- DaveA