Code Point
July 31, 2010, 10:59:28 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: help me with this script, just for fun.  (Read 4210 times)
fuston05
Jr. Member
*

Karma: 0
Offline Offline

Posts: 14


« on: February 07, 2008, 09:50:32 PM »

my wife likes to break names down into #'s, by assigning a # value to each letter determined by its natural numerical place in the alphabet. just for practice i was writing a program to automate this process for her.
so far i got:
print"This program will break down a persons name into #'s for Angela."

raw_input("hit enter to continue")

a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=8
i=9
j=10
k=11
l=12
m=13
n=14
o=15
p=16
q=17
r=18
s=19
t=20
u=21
v=22
w=23
x=24
y=25
z=26

name=raw_input("What is the name you wish to annalize? Please use lower case letters only")

1st question: is there an easier way to assign values to each letter in alphabet?

2nd question: assuming im not heading entirely in the wrong direction with this script, haw would i assign afunction to the user inout to make it return a # value for "each" letter in a given name that might be entered?

ex. of intended function:   user entered: bob 
 return shud be: 2, 15, 2
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #1 on: February 07, 2008, 11:19:21 PM »

1st question: is there an easier way to assign values to each letter in alphabet?

Sure. The most common encoding is the ASCII. Just type ASCII in a search engine to see the ASCII table.

Python can tell the ASCII code for a given character:
Code
>>> ord('a')
97
>>> ord('A')
65
>>> ord('0')
48
 

2nd question: assuming im not heading entirely in the wrong direction with this script, haw would i assign afunction to the user inout to make it return a # value for "each" letter in a given name that might be entered?

ex. of intended function:   user entered: bob 
 return shud be: 2, 15, 2

Use a for loop to loop over the characters of the name. For each character use the ord() function to print its ASCII code.
Logged
fuston05
Jr. Member
*

Karma: 0
Offline Offline

Posts: 14


« Reply #2 on: February 08, 2008, 03:28:16 AM »

you are awesome, ty yet again
Logged
fuston05
Jr. Member
*

Karma: 0
Offline Offline

Posts: 14


« Reply #3 on: February 08, 2008, 10:43:43 PM »

instead of using the "ascII" is there a way i can "assign my own numeric values to each individual letter throughout the alphabet?
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #4 on: February 08, 2008, 11:44:45 PM »

You can create a dictionary:

Code
d = dict()
d['a'] = 1
d['b'] = 2
print d
 
e = {'a':1, 'b':2}
print e
 

Or do it programaticaly:

Code
my_encoding = dict()
for c in range(1, 59):
   char = chr(c + 64)
   my_encoding[char] = c
   print "dict['%s'] = %s" % (char, my_encoding[char])
 
Logged
fuston05
Jr. Member
*

Karma: 0
Offline Offline

Posts: 14


« Reply #5 on: February 08, 2008, 11:50:02 PM »

thanx alot  Smiley
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!