Error when generating dictionary from 2 lists
I have some code that takes a string input,
translates it to a list of integers using a dictionary
then generates a dict from that list and a list of the alphabet
(I would like to use the alphabet list as the key)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','p','q','r',
's','t','u','v','w','x','y','z',' ','.','-','-',',','!','?']
def gen_dict() :
key_dict = {'a':'00', 'b':'01', 'c':'02', 'd':'03', 'e':'04', 'f':'05',
'g':'06', 'h':'07', 'i':'08', 'j':'09', 'k':'10', 'l':'11',
'm':'12', 'n':'13', 'o':'14', 'p':'15', 'q':'16', 'r':'17',
's':'18', 't':'19', 'u':'20', 'v':'21', 'w':'22', 'x':'23',
'y':'24', 'z':'25', ' ':'26', '.':'27', '-':'28', ',':'29',
'!':'30', '?':'31'}
print(key_dict)
p= 0
key = key.lower()
for character in key:
keyint.append(key_dict[message[p]])
p += 1
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
print(letter-num_dict)
key = input()
gen_dict()
but the program won't even run,
instead it gives the error message:
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
^ SyntaxError: can't assign to operator
I couldn't find anything else online getting this error message for similar reasons so any help is appreciated.
python list dictionary syntax-error assignment-operator
add a comment |
I have some code that takes a string input,
translates it to a list of integers using a dictionary
then generates a dict from that list and a list of the alphabet
(I would like to use the alphabet list as the key)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','p','q','r',
's','t','u','v','w','x','y','z',' ','.','-','-',',','!','?']
def gen_dict() :
key_dict = {'a':'00', 'b':'01', 'c':'02', 'd':'03', 'e':'04', 'f':'05',
'g':'06', 'h':'07', 'i':'08', 'j':'09', 'k':'10', 'l':'11',
'm':'12', 'n':'13', 'o':'14', 'p':'15', 'q':'16', 'r':'17',
's':'18', 't':'19', 'u':'20', 'v':'21', 'w':'22', 'x':'23',
'y':'24', 'z':'25', ' ':'26', '.':'27', '-':'28', ',':'29',
'!':'30', '?':'31'}
print(key_dict)
p= 0
key = key.lower()
for character in key:
keyint.append(key_dict[message[p]])
p += 1
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
print(letter-num_dict)
key = input()
gen_dict()
but the program won't even run,
instead it gives the error message:
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
^ SyntaxError: can't assign to operator
I couldn't find anything else online getting this error message for similar reasons so any help is appreciated.
python list dictionary syntax-error assignment-operator
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Python cannot use variable names with an operator in them:letter-num_dictis as invalid as, say,10ora+b, for a variable name.
– usr2564301
Nov 24 '18 at 14:02
add a comment |
I have some code that takes a string input,
translates it to a list of integers using a dictionary
then generates a dict from that list and a list of the alphabet
(I would like to use the alphabet list as the key)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','p','q','r',
's','t','u','v','w','x','y','z',' ','.','-','-',',','!','?']
def gen_dict() :
key_dict = {'a':'00', 'b':'01', 'c':'02', 'd':'03', 'e':'04', 'f':'05',
'g':'06', 'h':'07', 'i':'08', 'j':'09', 'k':'10', 'l':'11',
'm':'12', 'n':'13', 'o':'14', 'p':'15', 'q':'16', 'r':'17',
's':'18', 't':'19', 'u':'20', 'v':'21', 'w':'22', 'x':'23',
'y':'24', 'z':'25', ' ':'26', '.':'27', '-':'28', ',':'29',
'!':'30', '?':'31'}
print(key_dict)
p= 0
key = key.lower()
for character in key:
keyint.append(key_dict[message[p]])
p += 1
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
print(letter-num_dict)
key = input()
gen_dict()
but the program won't even run,
instead it gives the error message:
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
^ SyntaxError: can't assign to operator
I couldn't find anything else online getting this error message for similar reasons so any help is appreciated.
python list dictionary syntax-error assignment-operator
I have some code that takes a string input,
translates it to a list of integers using a dictionary
then generates a dict from that list and a list of the alphabet
(I would like to use the alphabet list as the key)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','p','q','r',
's','t','u','v','w','x','y','z',' ','.','-','-',',','!','?']
def gen_dict() :
key_dict = {'a':'00', 'b':'01', 'c':'02', 'd':'03', 'e':'04', 'f':'05',
'g':'06', 'h':'07', 'i':'08', 'j':'09', 'k':'10', 'l':'11',
'm':'12', 'n':'13', 'o':'14', 'p':'15', 'q':'16', 'r':'17',
's':'18', 't':'19', 'u':'20', 'v':'21', 'w':'22', 'x':'23',
'y':'24', 'z':'25', ' ':'26', '.':'27', '-':'28', ',':'29',
'!':'30', '?':'31'}
print(key_dict)
p= 0
key = key.lower()
for character in key:
keyint.append(key_dict[message[p]])
p += 1
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
print(letter-num_dict)
key = input()
gen_dict()
but the program won't even run,
instead it gives the error message:
letter-num_dict = {k:v for k,v in zip(alphabet,keyint)}
^ SyntaxError: can't assign to operator
I couldn't find anything else online getting this error message for similar reasons so any help is appreciated.
python list dictionary syntax-error assignment-operator
python list dictionary syntax-error assignment-operator
edited Nov 24 '18 at 14:53
petezurich
3,50581734
3,50581734
asked Nov 24 '18 at 13:59
LokiTheGAMERLokiTheGAMER
163
163
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Python cannot use variable names with an operator in them:letter-num_dictis as invalid as, say,10ora+b, for a variable name.
– usr2564301
Nov 24 '18 at 14:02
add a comment |
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Python cannot use variable names with an operator in them:letter-num_dictis as invalid as, say,10ora+b, for a variable name.
– usr2564301
Nov 24 '18 at 14:02
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Python cannot use variable names with an operator in them:
letter-num_dict is as invalid as, say, 10 or a+b, for a variable name.– usr2564301
Nov 24 '18 at 14:02
Python cannot use variable names with an operator in them:
letter-num_dict is as invalid as, say, 10 or a+b, for a variable name.– usr2564301
Nov 24 '18 at 14:02
add a comment |
2 Answers
2
active
oldest
votes
You can't use hyphens in variable names.
Change letter-num_dict to letter_num_dict (or equivalent).
add a comment |
Variables cannot have a dash (-) as a part of the name, as Python interprets that as the minus operator. Change it to an underscore instead: letter_num_dict
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458904%2ferror-when-generating-dictionary-from-2-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't use hyphens in variable names.
Change letter-num_dict to letter_num_dict (or equivalent).
add a comment |
You can't use hyphens in variable names.
Change letter-num_dict to letter_num_dict (or equivalent).
add a comment |
You can't use hyphens in variable names.
Change letter-num_dict to letter_num_dict (or equivalent).
You can't use hyphens in variable names.
Change letter-num_dict to letter_num_dict (or equivalent).
answered Nov 24 '18 at 14:02
Joe IddonJoe Iddon
15k31538
15k31538
add a comment |
add a comment |
Variables cannot have a dash (-) as a part of the name, as Python interprets that as the minus operator. Change it to an underscore instead: letter_num_dict
add a comment |
Variables cannot have a dash (-) as a part of the name, as Python interprets that as the minus operator. Change it to an underscore instead: letter_num_dict
add a comment |
Variables cannot have a dash (-) as a part of the name, as Python interprets that as the minus operator. Change it to an underscore instead: letter_num_dict
Variables cannot have a dash (-) as a part of the name, as Python interprets that as the minus operator. Change it to an underscore instead: letter_num_dict
answered Nov 24 '18 at 14:02
Jonah BishopJonah Bishop
8,76233057
8,76233057
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458904%2ferror-when-generating-dictionary-from-2-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please use the Preview to check if your question looks alright before posting it. (It does not. You need to format your code correctly.)
– usr2564301
Nov 24 '18 at 14:01
Python cannot use variable names with an operator in them:
letter-num_dictis as invalid as, say,10ora+b, for a variable name.– usr2564301
Nov 24 '18 at 14:02