Python 3 Jump Tables












-1














I am trying to figure out how to create a basic jump table, so I can better understand different ways of creating menus in Python 3.5.6. Here is what I have so far:



def command():
selection = input("Please enter your selection: ")
return selection

def one():
print ("you have selected menu option one")

def two():
print ("you have selected menu option two")

def three():
print ("you have selected menu option three")

def runCommand(command):
jumpTable = 0
jumpTable[command]()
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three

def main():
command()
runCommand(command)

if __name__ == "__main__":
main()


As far as I understand, a jump table is simply a way of making a menu selection and calling a specific function associated with that numerical value, taken in by my "command" function. Within the jumpTable, you assign the function to call.



I am getting " File "omitted", line 16, in runCommandjumpTableone
TypeError: 'int' object is not subscriptable



All I want to do is have the user enter a number - 1, 2 or 3 and have that function run. when I get this basic functionality down, I will expand the menu to show the options and be more clear. I just need to get the darn thing to run!



Yes, I am aware of other ways to create menus (IF/ELIF/ELSE) I am just trying to nail this one down!



Thank you in advance!










share|improve this question


















  • 1




    You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
    – MisterMiyagi
    Nov 19 '18 at 19:41


















-1














I am trying to figure out how to create a basic jump table, so I can better understand different ways of creating menus in Python 3.5.6. Here is what I have so far:



def command():
selection = input("Please enter your selection: ")
return selection

def one():
print ("you have selected menu option one")

def two():
print ("you have selected menu option two")

def three():
print ("you have selected menu option three")

def runCommand(command):
jumpTable = 0
jumpTable[command]()
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three

def main():
command()
runCommand(command)

if __name__ == "__main__":
main()


As far as I understand, a jump table is simply a way of making a menu selection and calling a specific function associated with that numerical value, taken in by my "command" function. Within the jumpTable, you assign the function to call.



I am getting " File "omitted", line 16, in runCommandjumpTableone
TypeError: 'int' object is not subscriptable



All I want to do is have the user enter a number - 1, 2 or 3 and have that function run. when I get this basic functionality down, I will expand the menu to show the options and be more clear. I just need to get the darn thing to run!



Yes, I am aware of other ways to create menus (IF/ELIF/ELSE) I am just trying to nail this one down!



Thank you in advance!










share|improve this question


















  • 1




    You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
    – MisterMiyagi
    Nov 19 '18 at 19:41
















-1












-1








-1







I am trying to figure out how to create a basic jump table, so I can better understand different ways of creating menus in Python 3.5.6. Here is what I have so far:



def command():
selection = input("Please enter your selection: ")
return selection

def one():
print ("you have selected menu option one")

def two():
print ("you have selected menu option two")

def three():
print ("you have selected menu option three")

def runCommand(command):
jumpTable = 0
jumpTable[command]()
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three

def main():
command()
runCommand(command)

if __name__ == "__main__":
main()


As far as I understand, a jump table is simply a way of making a menu selection and calling a specific function associated with that numerical value, taken in by my "command" function. Within the jumpTable, you assign the function to call.



I am getting " File "omitted", line 16, in runCommandjumpTableone
TypeError: 'int' object is not subscriptable



All I want to do is have the user enter a number - 1, 2 or 3 and have that function run. when I get this basic functionality down, I will expand the menu to show the options and be more clear. I just need to get the darn thing to run!



Yes, I am aware of other ways to create menus (IF/ELIF/ELSE) I am just trying to nail this one down!



Thank you in advance!










share|improve this question













I am trying to figure out how to create a basic jump table, so I can better understand different ways of creating menus in Python 3.5.6. Here is what I have so far:



def command():
selection = input("Please enter your selection: ")
return selection

def one():
print ("you have selected menu option one")

def two():
print ("you have selected menu option two")

def three():
print ("you have selected menu option three")

def runCommand(command):
jumpTable = 0
jumpTable[command]()
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three

def main():
command()
runCommand(command)

if __name__ == "__main__":
main()


As far as I understand, a jump table is simply a way of making a menu selection and calling a specific function associated with that numerical value, taken in by my "command" function. Within the jumpTable, you assign the function to call.



I am getting " File "omitted", line 16, in runCommandjumpTableone
TypeError: 'int' object is not subscriptable



All I want to do is have the user enter a number - 1, 2 or 3 and have that function run. when I get this basic functionality down, I will expand the menu to show the options and be more clear. I just need to get the darn thing to run!



Yes, I am aware of other ways to create menus (IF/ELIF/ELSE) I am just trying to nail this one down!



Thank you in advance!







python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 19:37









Larry FitzgeraldLarry Fitzgerald

63




63








  • 1




    You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
    – MisterMiyagi
    Nov 19 '18 at 19:41
















  • 1




    You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
    – MisterMiyagi
    Nov 19 '18 at 19:41










1




1




You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
– MisterMiyagi
Nov 19 '18 at 19:41






You initialize jumptable to 1 instead of a container , then try to index it. As the error already said, that is not valid. You have to properly build your jump table before using it.
– MisterMiyagi
Nov 19 '18 at 19:41














1 Answer
1






active

oldest

votes


















1














You are quite close. The only issue is that you are trying to access the command before creating the jumpTable. And I am also not sure why you are setting the variable to 0 first (that's why you get the int is not subscriptible error). So, this is the right order:



def runCommand(command):
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three
jumpTable[command]()


By the way, if you are always creating the same jumpTable, you could create it once, outside the function and simply call jumpTable[command]() in your main function.



Another problem: you should store the value you get from the user and pass that to the next function like this:



cmd = command()
runCommand(cmd)


, or simply pipe the two functions together like this:



runCommand(command())





share|improve this answer





















  • KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
    – Larry Fitzgerald
    Nov 19 '18 at 20:23












  • Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
    – zsomko
    Nov 19 '18 at 21:20











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381491%2fpython-3-jump-tables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You are quite close. The only issue is that you are trying to access the command before creating the jumpTable. And I am also not sure why you are setting the variable to 0 first (that's why you get the int is not subscriptible error). So, this is the right order:



def runCommand(command):
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three
jumpTable[command]()


By the way, if you are always creating the same jumpTable, you could create it once, outside the function and simply call jumpTable[command]() in your main function.



Another problem: you should store the value you get from the user and pass that to the next function like this:



cmd = command()
runCommand(cmd)


, or simply pipe the two functions together like this:



runCommand(command())





share|improve this answer





















  • KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
    – Larry Fitzgerald
    Nov 19 '18 at 20:23












  • Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
    – zsomko
    Nov 19 '18 at 21:20
















1














You are quite close. The only issue is that you are trying to access the command before creating the jumpTable. And I am also not sure why you are setting the variable to 0 first (that's why you get the int is not subscriptible error). So, this is the right order:



def runCommand(command):
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three
jumpTable[command]()


By the way, if you are always creating the same jumpTable, you could create it once, outside the function and simply call jumpTable[command]() in your main function.



Another problem: you should store the value you get from the user and pass that to the next function like this:



cmd = command()
runCommand(cmd)


, or simply pipe the two functions together like this:



runCommand(command())





share|improve this answer





















  • KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
    – Larry Fitzgerald
    Nov 19 '18 at 20:23












  • Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
    – zsomko
    Nov 19 '18 at 21:20














1












1








1






You are quite close. The only issue is that you are trying to access the command before creating the jumpTable. And I am also not sure why you are setting the variable to 0 first (that's why you get the int is not subscriptible error). So, this is the right order:



def runCommand(command):
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three
jumpTable[command]()


By the way, if you are always creating the same jumpTable, you could create it once, outside the function and simply call jumpTable[command]() in your main function.



Another problem: you should store the value you get from the user and pass that to the next function like this:



cmd = command()
runCommand(cmd)


, or simply pipe the two functions together like this:



runCommand(command())





share|improve this answer












You are quite close. The only issue is that you are trying to access the command before creating the jumpTable. And I am also not sure why you are setting the variable to 0 first (that's why you get the int is not subscriptible error). So, this is the right order:



def runCommand(command):
jumpTable = {}
jumpTable['1'] = one
jumpTable['2'] = two
jumpTable['3'] = three
jumpTable[command]()


By the way, if you are always creating the same jumpTable, you could create it once, outside the function and simply call jumpTable[command]() in your main function.



Another problem: you should store the value you get from the user and pass that to the next function like this:



cmd = command()
runCommand(cmd)


, or simply pipe the two functions together like this:



runCommand(command())






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:48









zsomkozsomko

4966




4966












  • KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
    – Larry Fitzgerald
    Nov 19 '18 at 20:23












  • Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
    – zsomko
    Nov 19 '18 at 21:20


















  • KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
    – Larry Fitzgerald
    Nov 19 '18 at 20:23












  • Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
    – zsomko
    Nov 19 '18 at 21:20
















KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
– Larry Fitzgerald
Nov 19 '18 at 20:23






KeyError: <function command at 0x0000000001E15400> So now apparently it cannot find the key value, so it spits out a memory location... In the past, I would always build my dictionaries so I would know the value. In this case it seems it's built on the fly. I adjusted my code with what you said zsomko.
– Larry Fitzgerald
Nov 19 '18 at 20:23














Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
– zsomko
Nov 19 '18 at 21:20




Your problem is now what I have described in the part after "Another problem" in my reply. You are trying to index your dictionary with the function named command, not the return value of that function.
– zsomko
Nov 19 '18 at 21:20


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381491%2fpython-3-jump-tables%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter