By Joey


2018-05-16 15:02:04 8 Comments

This is my code:

import string
values = dict()
user_entry=raw_input("Enter: ")
for index, letter in enumerate(string.ascii_lowercase):
    values[letter] = index + 1

print values[user_entry] 

I am trying to get a number for each letter from a word the user enters. For example, if the user enters "code" then I need to assign a number to c, o, d, and e. For c it would be 3, for o it would be 15, for d it would be 4, and for e it would be 5. I tried to separate them by using list so that I can split each letter to get a number for each letter but nothing seems to work. I need some help!!

1 comments

@zipa 2018-05-16 15:08:02

Was this what you were looking for:

import string

values = {v: k for k, v in enumerate(string.ascii_lowercase, 1)}

user_entry=raw_input("Enter: ")

print([values[i] for i in user_entry])
#[3, 15, 4, 5]

And another way would be:

import string

low = string.ascii_lowercase

user_entry=raw_input("Enter: ")

print([low.index(i)+1 for i in user_entry])

@Joey 2018-05-16 15:11:53

That's exactly what I was looking for but is there any other way to do it? Just wanted to know. Thank you!!!

@jpp 2018-05-16 15:11:56

Or map(values.get, user_entry). @Joey, no I don't think there is a better way.

@Joey 2018-05-16 15:12:48

Alright but I really apreciate your answer tho

@zipa 2018-05-16 15:12:51

@jpp or without list(), it's Python 2.x :)

@Joey 2018-05-16 15:13:28

what does v:k do in here?

@zipa 2018-05-16 15:14:24

@Joey Inverts the dict from 1: 'a' to 'a':1

@zipa 2018-05-16 15:16:30

@Joey Added example without dict

@Joey 2018-05-16 15:16:53

What is the purpose of i here I've never understood it. Why doesn't it also says that i is undefined?

@zipa 2018-05-16 15:18:54

@Joey That pattern is called list comprehension

@Joey 2018-05-16 15:23:07

Last question I promise. Can I get an answer in an integer form not a list?

@zipa 2018-05-16 15:24:17

@Joey Like a string '31545'?

@Joey 2018-05-16 15:25:48

like this: 3 15 4 5

@Joey 2018-05-16 15:26:15

and i still want to use that answer in my further program

@Joey 2018-05-16 15:26:30

so that's why i was asking

@zipa 2018-05-16 15:30:55

@Joey Wouldn't list be better? Because 3 15 4 5 is not an integer.

@Joey 2018-05-16 15:40:29

let's say I have a list of xyz=["q","w","e","r","t","y"] and what I need to do is find the 3rd letter in the xyz list then the 15th letter and so on depending on what the user inputs.

@Joey 2018-05-16 15:44:59

please help zipa!!

@zipa 2018-05-16 15:55:16

Well this code does it, you can maybe propose another question with more details and I'll be glad to help you

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 2289655 View
  • 2654 Score
  • 31 Answer
  • Tags:   python directory

38 Answered Questions

[SOLVED] How do I sort a dictionary by value?

26 Answered Questions

[SOLVED] How can I create a directory if it does not exist?

26 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 1636699 View
  • 2603 Score
  • 26 Answer
  • Tags:   python list

48 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

6 Answered Questions

[SOLVED] How to get the number of elements in a list in Python?

  • 2009-11-11 00:30:54
  • y2k
  • 2332185 View
  • 1464 Score
  • 6 Answer
  • Tags:   python list

39 Answered Questions

[SOLVED] How to check whether a file exists?

  • 2008-09-17 12:55:00
  • spence91
  • 2762153 View
  • 4190 Score
  • 39 Answer
  • Tags:   python file

10 Answered Questions

13 Answered Questions

[SOLVED] How do you return multiple values in Python?

7 Answered Questions

[SOLVED] Catch multiple exceptions in one line (except block)

Sponsored Content