I'm making a dictionary in which the keys will be one-character symbols separates by a space that the user inputs. I'm assuming that I have call the ord value in a separate line but I'm not sure how to correctly phrase it. Below is the code I have currently:
inp = input()
inp = inp.split(" ")
d = dict.fromkeys(inp, ord(inp))
print(d)
CodePudding user response:
A dictionary comprehension would give you what you want:
inp = input()
d = {x: ord(x) for x in inp}
print(d)
This iterates through the string inp
and constructs the key/value pairs from the iterator, x
, and ord(x)
.
This doesn't remove spaces, as you seem to possibly be after by inp.split(" ")
, but I interpreted this as your attempt to split the string by character. Should you want to remove spaces, remove them from the inp
object.
CodePudding user response:
inp = input()
inp = inp.split(" ")
ordinput = []
for i in inp:
x = ord(i)
ordinput.append(x)
d = dict(zip(inp, ordinput))
print(d)