Domanda

I've got this function that takes a name as an input, puts it into a list and then runs ord() against it. However, I am having some (what I believe) to be formatting issues. I'm trying to get it to appear like so:

b = (ascii value)
a = (ascii value)
t = (ascii value)
m = (ascii value)
a = (ascii value)
n = (ascii value)

I've got the name appearing correctly, however the ascii value is appearing like this:

b = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
t = [98, 97, 116, 109, 97, 110]
m = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
n = [98, 97, 116, 109, 97, 110]

Not sure where I'm going wrong, below is the code that I've made for it:

def x():
     name = requestString("name")
     usersName = list(name)
     ascii = [orc(c) for c in usersName]
     for name in name: 
          print name, "=", ascii 

Thanks!

edit: Thanks, it's really appreciated. Get where I went wrong now!

È stato utile?

Soluzione

Here's a bit of review of where you went wrong:

def x():
     name = requestString("name")
     usersName = list(name)
     ascii = [orc(c) for c in usersName] # here's the list
     for name in name: 
          print name, "=", ascii # and you're printing it here everytime

You could fix more pythonically like this:

def x():
     name = requestString("name")
     # usersName = list(name) # no need for this line, you can iterate over the string
     ascii = [orc(c) for c in name] #so this is just name
     for i, c in enumerate(name):  # use c for your character var name, 
          print c, "=", ascii[i]   # and enumerate provides the index

Since you're not returning anything, creating a list is unnecessary, you might as well provide the ord(c) on the fly:

def print_ords_of_word(name):
    for c in name:
        print c, '=', ord(c)

Altri suggerimenti

ascii is the list of ord for all characters. To pair them up with the characters they represent, use zip:

for num, char in zip(ascii, name):
    print "'{0}'={1}".format(char, num)

You can make it in a single loop:

for item in name:
    print item, "=", ord(item)

Demo:

>>> def x(name):
...      for item in name:
...           print item, "=", ord(item)
... 
>>> x('batman')
b = 98
a = 97
t = 116
m = 109
a = 97
n = 110

[ord(c) for c in usersName] is a list of ALL the ord values for the letters in usersName. That's not what you want to put next to your letter -- you want to put JUST THAT one.

You can do this:

def x():
    name = requestString("name")
    ascii = {letter:ord(letter) for letter in name}
    for letter in name:
        print(letter+" = "+ascii[letter])
        # or use string formatting which is better

Or alternatively just:

def x():
    name = requestString("name")
    for letter in name:
        print(letter+" = "+ord(letter))

You are getting a list back because you used a list comprehension to convert the entire usersName into a list of ords. This could be written in a single line with:

print "\n".join("{0} = {1}".format(c, ord(c)) for c in name)

The join method on a string takes a sequence, or an iterator, and delimits each item with the original contents of the string (in this case \n).

The for loop inside the call to join creates a generator, you can think of as a list comprehension, if it helps you understand better (but it really isn't).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top