How to pass vairable to get() in dicitonary in python?










-2















I am simply trying to solve the following question



You are given a date. Your task is to find what the day is on that date.
Sample imput : 08 10 2018



I came up with following solution:



days='0':'monday','1':'tuesday','2':'wed','3':'thurs','4':'friday','5':'sat','6':'sun'

user_input=datetime.datetime(2018, 11 ,6)

result=user_input.weekday()
print (days.get('result'))


Now it prints 'None' ,I was expecting this to return keys value.



Even print(result) returns value of 1 and type(result) returns int.



I want to know why I am not getting the expected output.
There must be a more pythonic way to do this but my major concern is the way get() is behaving.










share|improve this question

















  • 4





    I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

    – khelwood
    Nov 13 '18 at 14:18












  • try print (days.get(result)) without the quotes.

    – Ev. Kounis
    Nov 13 '18 at 14:18











  • days.get(result).

    – Austin
    Nov 13 '18 at 14:19











  • Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

    – myrmica
    Nov 13 '18 at 14:23
















-2















I am simply trying to solve the following question



You are given a date. Your task is to find what the day is on that date.
Sample imput : 08 10 2018



I came up with following solution:



days='0':'monday','1':'tuesday','2':'wed','3':'thurs','4':'friday','5':'sat','6':'sun'

user_input=datetime.datetime(2018, 11 ,6)

result=user_input.weekday()
print (days.get('result'))


Now it prints 'None' ,I was expecting this to return keys value.



Even print(result) returns value of 1 and type(result) returns int.



I want to know why I am not getting the expected output.
There must be a more pythonic way to do this but my major concern is the way get() is behaving.










share|improve this question

















  • 4





    I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

    – khelwood
    Nov 13 '18 at 14:18












  • try print (days.get(result)) without the quotes.

    – Ev. Kounis
    Nov 13 '18 at 14:18











  • days.get(result).

    – Austin
    Nov 13 '18 at 14:19











  • Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

    – myrmica
    Nov 13 '18 at 14:23














-2












-2








-2








I am simply trying to solve the following question



You are given a date. Your task is to find what the day is on that date.
Sample imput : 08 10 2018



I came up with following solution:



days='0':'monday','1':'tuesday','2':'wed','3':'thurs','4':'friday','5':'sat','6':'sun'

user_input=datetime.datetime(2018, 11 ,6)

result=user_input.weekday()
print (days.get('result'))


Now it prints 'None' ,I was expecting this to return keys value.



Even print(result) returns value of 1 and type(result) returns int.



I want to know why I am not getting the expected output.
There must be a more pythonic way to do this but my major concern is the way get() is behaving.










share|improve this question














I am simply trying to solve the following question



You are given a date. Your task is to find what the day is on that date.
Sample imput : 08 10 2018



I came up with following solution:



days='0':'monday','1':'tuesday','2':'wed','3':'thurs','4':'friday','5':'sat','6':'sun'

user_input=datetime.datetime(2018, 11 ,6)

result=user_input.weekday()
print (days.get('result'))


Now it prints 'None' ,I was expecting this to return keys value.



Even print(result) returns value of 1 and type(result) returns int.



I want to know why I am not getting the expected output.
There must be a more pythonic way to do this but my major concern is the way get() is behaving.







python dictionary scripting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:16









RustyRusty

1056




1056







  • 4





    I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

    – khelwood
    Nov 13 '18 at 14:18












  • try print (days.get(result)) without the quotes.

    – Ev. Kounis
    Nov 13 '18 at 14:18











  • days.get(result).

    – Austin
    Nov 13 '18 at 14:19











  • Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

    – myrmica
    Nov 13 '18 at 14:23













  • 4





    I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

    – khelwood
    Nov 13 '18 at 14:18












  • try print (days.get(result)) without the quotes.

    – Ev. Kounis
    Nov 13 '18 at 14:18











  • days.get(result).

    – Austin
    Nov 13 '18 at 14:19











  • Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

    – myrmica
    Nov 13 '18 at 14:23








4




4





I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

– khelwood
Nov 13 '18 at 14:18






I think you mean days.get(result) not days.get('result'). The string 'result' is not in your dictionary. Though maybe the keys in your dictionary should be ints, not strings.

– khelwood
Nov 13 '18 at 14:18














try print (days.get(result)) without the quotes.

– Ev. Kounis
Nov 13 '18 at 14:18





try print (days.get(result)) without the quotes.

– Ev. Kounis
Nov 13 '18 at 14:18













days.get(result).

– Austin
Nov 13 '18 at 14:19





days.get(result).

– Austin
Nov 13 '18 at 14:19













Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

– myrmica
Nov 13 '18 at 14:23






Either print(days[str(result)]) or change the dictionary keys to integers and then print(days[result])

– myrmica
Nov 13 '18 at 14:23













1 Answer
1






active

oldest

votes


















1














Firstly, days.get('result') is trying to look up the string 'result' in your dictionary, not the variable result.



Secondly user_input.weekday() returns an int, but the keys in your dictionary are strings, not ints.



This would have worked:



days = 0:'monday', 1:'tuesday', 2:'wed', 3:'thurs', 4:'friday', 5:'sat', 6:'sun'
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days.get(result)) # or simply print(days[result])


However, since the possible range of days is the numbers 0 to 6, a list would work just as well.



days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days[result])





share|improve this answer























  • I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

    – Rusty
    Nov 13 '18 at 14:50











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%2f53283017%2fhow-to-pass-vairable-to-get-in-dicitonary-in-python%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














Firstly, days.get('result') is trying to look up the string 'result' in your dictionary, not the variable result.



Secondly user_input.weekday() returns an int, but the keys in your dictionary are strings, not ints.



This would have worked:



days = 0:'monday', 1:'tuesday', 2:'wed', 3:'thurs', 4:'friday', 5:'sat', 6:'sun'
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days.get(result)) # or simply print(days[result])


However, since the possible range of days is the numbers 0 to 6, a list would work just as well.



days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days[result])





share|improve this answer























  • I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

    – Rusty
    Nov 13 '18 at 14:50
















1














Firstly, days.get('result') is trying to look up the string 'result' in your dictionary, not the variable result.



Secondly user_input.weekday() returns an int, but the keys in your dictionary are strings, not ints.



This would have worked:



days = 0:'monday', 1:'tuesday', 2:'wed', 3:'thurs', 4:'friday', 5:'sat', 6:'sun'
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days.get(result)) # or simply print(days[result])


However, since the possible range of days is the numbers 0 to 6, a list would work just as well.



days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days[result])





share|improve this answer























  • I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

    – Rusty
    Nov 13 '18 at 14:50














1












1








1







Firstly, days.get('result') is trying to look up the string 'result' in your dictionary, not the variable result.



Secondly user_input.weekday() returns an int, but the keys in your dictionary are strings, not ints.



This would have worked:



days = 0:'monday', 1:'tuesday', 2:'wed', 3:'thurs', 4:'friday', 5:'sat', 6:'sun'
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days.get(result)) # or simply print(days[result])


However, since the possible range of days is the numbers 0 to 6, a list would work just as well.



days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days[result])





share|improve this answer













Firstly, days.get('result') is trying to look up the string 'result' in your dictionary, not the variable result.



Secondly user_input.weekday() returns an int, but the keys in your dictionary are strings, not ints.



This would have worked:



days = 0:'monday', 1:'tuesday', 2:'wed', 3:'thurs', 4:'friday', 5:'sat', 6:'sun'
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days.get(result)) # or simply print(days[result])


However, since the possible range of days is the numbers 0 to 6, a list would work just as well.



days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = datetime.datetime(2018, 11, 6)
result = user_input.weekday()
print(days[result])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:25









khelwoodkhelwood

30.8k74263




30.8k74263












  • I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

    – Rusty
    Nov 13 '18 at 14:50


















  • I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

    – Rusty
    Nov 13 '18 at 14:50

















I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

– Rusty
Nov 13 '18 at 14:50






I was not aware that I am looking for result as key itself. I was given this question in sys admin Interview scripting part and Idk why dictionary came in mind ,I should have used list. Thanks for the help mate.

– Rusty
Nov 13 '18 at 14:50


















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283017%2fhow-to-pass-vairable-to-get-in-dicitonary-in-python%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto