-1

My question can be understood below:

goodvalue=False
while (goodvalue==False):
   try:
      word=str(input("Please enter a word: "))
   except ValueError:
       print ("Wrong Input...")
   else:
       goodvalue=True

word=word.lower()
List=list(map(str,word))
lenList=len(list(map(str,word)))
listofans=[]
x=0
while (lenList-1==x):
    if List[x]==str("a"):
        listofans[x]=str("1")
        x=x+1
    elif List[x]==str("b"):
        listofans[x]=str("2")
        x=x+1
    elif List[x]==str("c"):
        listofans[x]=str("3")
        x=x+1

It continues like that for all alphabets for a while... And then:

sumofnums=listofans[0]       
y=1
while (lenList-2==y):
    sumofnums+=listofans[y]

print ("The number is: ", sumofnums)

So basically, if I give hello, it should return 8 5 12 12 15. Any help is truly appreciated!

  • 1
    why do you use `str("1")` as it's already a string – MichaelvdNet Jul 23 '13 at 11:20
  • 2
    You should be using a dictionary for your translation, not enumerating it in massive if statement! – Codie CodeMonkey Jul 23 '13 at 11:21
  • 1
    What does the program do right now? Do you have a specific question? – ethan Jul 23 '13 at 11:40
  • I'd highly recommend you check out some Python tutorials, such as [Learn Python the Hard War](http://learnpythonthehardway.org/). There's a lot of things in your code that aren't really Pythonic (`while (goodvalue==False)` for example). – thegrinner Jul 23 '13 at 13:57

4 Answers4

2

Your code is very messy, and some of it isn't even needed (all those uses of map is not needed. Nor is the try/except structure)

Why not simplify it a bit ;).

>>> import string
>>> d = {j:i for i, j in enumerate(string.lowercase, 1)}
>>> for i in 'hello':
...     print d[i],
... 
8 5 12 12 15

Some problems with your code:

  • Don't compare booleans like that. Just do while goodvalue.

  • List=list(map(str,word)) is excessive. A simple List = list(word) is needed, but you probably won't even need this as you can iterate through strings (as I have shown above)

  • str("a") is pointless. "a" is already a string, thus str() does nothing here.

  • As I said before, the try/except is not needed. No input could cause a ValueError. (unless you meant int())

TerryA
  • 58,805
  • 11
  • 114
  • 143
2

Are looking for something like this?

[ord(letter)-ord('a')+1 for letter in word]

For "hello" this outputs:

[8, 5, 12, 12, 15]

The ord function returns the ascii ordinal value for the letter. Subtracting ord('a') rebases that to 0, but you have 'a' mapping to 1, so it has to be adjusted by 1.

Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45
0

First of all just for make your code smaller you have to look on a small stuff like, instead of print =="a" you print ==str("a"). That's wrong.

So here is your old while loop:

while (lenList-1==x):
    if List[x]==str("a"):
        listofans[x]=str("1")
        x=x+1
    elif List[x]==str("b"):
        listofans[x]=str("2")
        x=x+1
    elif List[x]==str("c"):
        listofans[x]=str("3")
        x=x+1

And here is new:

while (lenList-1==x):
    if List[x]=="a":
        listofans[x]="1"
        x=x+1
    elif List[x]=="b":
        listofans[x]="2"
        x=x+1
    elif List[x]=="c":
        listofans[x]="3"
        x=x+1

And about your question, here is a solution:

[ord(string)-ord('a')+1 for string in word]

If you print "hello" this will returns you:

[8, 5, 12, 12, 15]
Michael
  • 15,386
  • 36
  • 94
  • 143
0

Try this:

goodvalue=False
while (goodvalue==False):
   try:
      word=str(input("Please enter a word: "))
   except ValueError:
       print ("Wrong Input...")
   else:
       goodvalue=True

word=word.lower()
wordtofans=[]

for c in word:
    if c >= 'a' and c <= 'z':
        wordtofans.append( int(ord(c)-ord('a')+1) )

print wordtofans

You can directly iterate a string in a for loop, you don't have to convert your string to a list.

You have a control check here to ensure that only letters a..z and A..Z are converted into numbers.

Conversion from a string letter to a number is done using int(ord(c)-ord('a')+1) which uses ord function that will return a ASCII value for a supplied character.

nio
  • 5,141
  • 2
  • 24
  • 35