Unable to get desired result from “ if ” in pythonpython input() not working as expectedCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Getting the last element of a list in PythonHow do I get the number of elements in a list in Python?Does Python have a string 'contains' substring method?

Displaying an Estimated Execution Plan generates CXPACKET, PAGELATCH_SH, and LATCH_EX [ACCESS_METHODS_DATASET_PARENT] waits

Many one decreasing function?

LiOH hydrolysis of methyl 2,2-dimethoxyacetate not giving product?

My C Drive is full without reason

How does "politician" work as a job/career?

Appropriate age to involve kids in life changing decisions

How to get file name from inside a latex file?

Did any early RISC OS precursor run on the BBC Micro?

What’s the interaction between darkvision and the Eagle Aspect of the beast, if you have Darkvision past 100 feet?

Concatenate all values of the same XML element using XPath/XQuery

What does “two-bit (jerk)” mean?

Why doesn't a particle exert force on itself?

What is the meaning of "matter" in physics?

What is the Ancient One's mistake?

Why is the episode called "The Last of the Starks"?

The unknown and unexplained in science fiction

Why is the blank symbol not considered part of the input alphabet of a Turing machine?

Why was Gemini VIII terminated after recovering from the OAMS thruster failure?

Can a player choose to add detail and flavor to their character's spells and abilities?

Why did Dr. Strange keep looking into the future after the snap?

How do I give a darkroom course without negs from the attendees?

All of my Firefox add-ons have been disabled suddenly, how can I re-enable them?

Convert a huge txt-file into a dataset

What chord could the notes 'F A♭ E♭' form?



Unable to get desired result from “ if ” in python


python input() not working as expectedCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Getting the last element of a list in PythonHow do I get the number of elements in a list in Python?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have an assignment to simply print dates in calendar like format. Everything is done except when the user enter some alphabet(string) in input, console gives an error statement standing the fact that i have included the code in if statement.



This is the error i am getting in console:



Please enter the number/name of the month (in any case/form)
you want the program to display calendar of: jan
Traceback (most recent call last):

File "<ipython-input-16-8ac5bd6555cd>", line 1, in <module>
runfile('D:/Studies & Learnings/Programming/Python/Calendar.py', wdir='D:/Studies & Learnings/Programming/Python')

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "D:/Studies & Learnings/Programming/Python/Calendar.py", line 3, in <module>
month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

File "C:Python27libsite-packagesIPythonkernelzmqipkernel.py", line 364, in <lambda>
input = lambda prompt='': eval(raw_input(prompt))

File "<string>", line 1, in <module>

NameError: name 'jan' is not defined


I have just started learning python. And this is my very first code in python. So if I am doing something obviously wrong, kindly let me know instead of rating my question 'negative value'



Thank you all...



month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

month = str(month)

if(month == "1" or month == "jan" or month == "Jan" or month == "january" or month == "January"):

monthNumber = 1
monthName = "January"

elif(month == "2" or month == "feb" or month == "Feb" or month == "february" or month == "Februrary"):

monthNumber = 2
monthName = "February"









share|improve this question

















  • 3





    Use raw_input instead of input. month = str(month) line is not required as month is already a string.

    – Austin
    Mar 23 at 6:39












  • You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

    – Heath Raftery
    Mar 23 at 22:00

















0















I have an assignment to simply print dates in calendar like format. Everything is done except when the user enter some alphabet(string) in input, console gives an error statement standing the fact that i have included the code in if statement.



This is the error i am getting in console:



Please enter the number/name of the month (in any case/form)
you want the program to display calendar of: jan
Traceback (most recent call last):

File "<ipython-input-16-8ac5bd6555cd>", line 1, in <module>
runfile('D:/Studies & Learnings/Programming/Python/Calendar.py', wdir='D:/Studies & Learnings/Programming/Python')

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "D:/Studies & Learnings/Programming/Python/Calendar.py", line 3, in <module>
month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

File "C:Python27libsite-packagesIPythonkernelzmqipkernel.py", line 364, in <lambda>
input = lambda prompt='': eval(raw_input(prompt))

File "<string>", line 1, in <module>

NameError: name 'jan' is not defined


I have just started learning python. And this is my very first code in python. So if I am doing something obviously wrong, kindly let me know instead of rating my question 'negative value'



Thank you all...



month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

month = str(month)

if(month == "1" or month == "jan" or month == "Jan" or month == "january" or month == "January"):

monthNumber = 1
monthName = "January"

elif(month == "2" or month == "feb" or month == "Feb" or month == "february" or month == "Februrary"):

monthNumber = 2
monthName = "February"









share|improve this question

















  • 3





    Use raw_input instead of input. month = str(month) line is not required as month is already a string.

    – Austin
    Mar 23 at 6:39












  • You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

    – Heath Raftery
    Mar 23 at 22:00













0












0








0








I have an assignment to simply print dates in calendar like format. Everything is done except when the user enter some alphabet(string) in input, console gives an error statement standing the fact that i have included the code in if statement.



This is the error i am getting in console:



Please enter the number/name of the month (in any case/form)
you want the program to display calendar of: jan
Traceback (most recent call last):

File "<ipython-input-16-8ac5bd6555cd>", line 1, in <module>
runfile('D:/Studies & Learnings/Programming/Python/Calendar.py', wdir='D:/Studies & Learnings/Programming/Python')

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "D:/Studies & Learnings/Programming/Python/Calendar.py", line 3, in <module>
month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

File "C:Python27libsite-packagesIPythonkernelzmqipkernel.py", line 364, in <lambda>
input = lambda prompt='': eval(raw_input(prompt))

File "<string>", line 1, in <module>

NameError: name 'jan' is not defined


I have just started learning python. And this is my very first code in python. So if I am doing something obviously wrong, kindly let me know instead of rating my question 'negative value'



Thank you all...



month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

month = str(month)

if(month == "1" or month == "jan" or month == "Jan" or month == "january" or month == "January"):

monthNumber = 1
monthName = "January"

elif(month == "2" or month == "feb" or month == "Feb" or month == "february" or month == "Februrary"):

monthNumber = 2
monthName = "February"









share|improve this question














I have an assignment to simply print dates in calendar like format. Everything is done except when the user enter some alphabet(string) in input, console gives an error statement standing the fact that i have included the code in if statement.



This is the error i am getting in console:



Please enter the number/name of the month (in any case/form)
you want the program to display calendar of: jan
Traceback (most recent call last):

File "<ipython-input-16-8ac5bd6555cd>", line 1, in <module>
runfile('D:/Studies & Learnings/Programming/Python/Calendar.py', wdir='D:/Studies & Learnings/Programming/Python')

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 685, in runfile
execfile(filename, namespace)

File "C:Python27libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "D:/Studies & Learnings/Programming/Python/Calendar.py", line 3, in <module>
month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

File "C:Python27libsite-packagesIPythonkernelzmqipkernel.py", line 364, in <lambda>
input = lambda prompt='': eval(raw_input(prompt))

File "<string>", line 1, in <module>

NameError: name 'jan' is not defined


I have just started learning python. And this is my very first code in python. So if I am doing something obviously wrong, kindly let me know instead of rating my question 'negative value'



Thank you all...



month = input("nPlease enter the number/name of the month (in any case/form)nyou want the program to display calendar of:t")

month = str(month)

if(month == "1" or month == "jan" or month == "Jan" or month == "january" or month == "January"):

monthNumber = 1
monthName = "January"

elif(month == "2" or month == "feb" or month == "Feb" or month == "february" or month == "Februrary"):

monthNumber = 2
monthName = "February"






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 6:28









Salman AhmadSalman Ahmad

63




63







  • 3





    Use raw_input instead of input. month = str(month) line is not required as month is already a string.

    – Austin
    Mar 23 at 6:39












  • You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

    – Heath Raftery
    Mar 23 at 22:00












  • 3





    Use raw_input instead of input. month = str(month) line is not required as month is already a string.

    – Austin
    Mar 23 at 6:39












  • You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

    – Heath Raftery
    Mar 23 at 22:00







3




3





Use raw_input instead of input. month = str(month) line is not required as month is already a string.

– Austin
Mar 23 at 6:39






Use raw_input instead of input. month = str(month) line is not required as month is already a string.

– Austin
Mar 23 at 6:39














You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

– Heath Raftery
Mar 23 at 22:00





You've misidentified the problem, which makes it hard to ask the right question. Read the console output from the bottom. The first line gives you the error: NameError: name 'jan' is not defined. The remaining lines show where this error occurred. The first two lines are higher level than your code. The third is the line you care about: month = input(.... So your question is actually "Python 2 input causes name is not defined error". Stick that in search to go directly to your answer: stackoverflow.com/a/21250504/3697870

– Heath Raftery
Mar 23 at 22:00












2 Answers
2






active

oldest

votes


















0














In Python 2, the input() function, for reasons that must have made sense at the time, tries to evaluate what's typed as a Python expression. Thus, when you type jan into an input() prompt, it tries to find the variable jan. This is not what you want in this (or pretty much any other*) case; you should use raw_input() instead.



Note that in Python 3, which will be the only supported version starting in 2020, the input() function of Python 2 is removed, and the raw_input() function is renamed to just input().




* It might seem that when you want a number, input() makes sense; when the user types 2 to an input() prompt, you get an int, not a str. But there are massive security problems any time you run eval, which input() does, so you should still use int(raw_input()) instead.






share|improve this answer






























    2














    You need not use month=string(month) as the input is in string form only. Remove the line and code should work






    share|improve this answer

























    • Are you sure? This is in Python 2. Did you run and check?

      – Austin
      Mar 23 at 6:37











    • nope, this does not work either...

      – Salman Ahmad
      Mar 23 at 7:40











    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%2f55311217%2funable-to-get-desired-result-from-if-in-python%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









    0














    In Python 2, the input() function, for reasons that must have made sense at the time, tries to evaluate what's typed as a Python expression. Thus, when you type jan into an input() prompt, it tries to find the variable jan. This is not what you want in this (or pretty much any other*) case; you should use raw_input() instead.



    Note that in Python 3, which will be the only supported version starting in 2020, the input() function of Python 2 is removed, and the raw_input() function is renamed to just input().




    * It might seem that when you want a number, input() makes sense; when the user types 2 to an input() prompt, you get an int, not a str. But there are massive security problems any time you run eval, which input() does, so you should still use int(raw_input()) instead.






    share|improve this answer



























      0














      In Python 2, the input() function, for reasons that must have made sense at the time, tries to evaluate what's typed as a Python expression. Thus, when you type jan into an input() prompt, it tries to find the variable jan. This is not what you want in this (or pretty much any other*) case; you should use raw_input() instead.



      Note that in Python 3, which will be the only supported version starting in 2020, the input() function of Python 2 is removed, and the raw_input() function is renamed to just input().




      * It might seem that when you want a number, input() makes sense; when the user types 2 to an input() prompt, you get an int, not a str. But there are massive security problems any time you run eval, which input() does, so you should still use int(raw_input()) instead.






      share|improve this answer

























        0












        0








        0







        In Python 2, the input() function, for reasons that must have made sense at the time, tries to evaluate what's typed as a Python expression. Thus, when you type jan into an input() prompt, it tries to find the variable jan. This is not what you want in this (or pretty much any other*) case; you should use raw_input() instead.



        Note that in Python 3, which will be the only supported version starting in 2020, the input() function of Python 2 is removed, and the raw_input() function is renamed to just input().




        * It might seem that when you want a number, input() makes sense; when the user types 2 to an input() prompt, you get an int, not a str. But there are massive security problems any time you run eval, which input() does, so you should still use int(raw_input()) instead.






        share|improve this answer













        In Python 2, the input() function, for reasons that must have made sense at the time, tries to evaluate what's typed as a Python expression. Thus, when you type jan into an input() prompt, it tries to find the variable jan. This is not what you want in this (or pretty much any other*) case; you should use raw_input() instead.



        Note that in Python 3, which will be the only supported version starting in 2020, the input() function of Python 2 is removed, and the raw_input() function is renamed to just input().




        * It might seem that when you want a number, input() makes sense; when the user types 2 to an input() prompt, you get an int, not a str. But there are massive security problems any time you run eval, which input() does, so you should still use int(raw_input()) instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 8:39









        Daniel HDaniel H

        5,45211831




        5,45211831























            2














            You need not use month=string(month) as the input is in string form only. Remove the line and code should work






            share|improve this answer

























            • Are you sure? This is in Python 2. Did you run and check?

              – Austin
              Mar 23 at 6:37











            • nope, this does not work either...

              – Salman Ahmad
              Mar 23 at 7:40















            2














            You need not use month=string(month) as the input is in string form only. Remove the line and code should work






            share|improve this answer

























            • Are you sure? This is in Python 2. Did you run and check?

              – Austin
              Mar 23 at 6:37











            • nope, this does not work either...

              – Salman Ahmad
              Mar 23 at 7:40













            2












            2








            2







            You need not use month=string(month) as the input is in string form only. Remove the line and code should work






            share|improve this answer















            You need not use month=string(month) as the input is in string form only. Remove the line and code should work







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 23 at 8:24

























            answered Mar 23 at 6:31









            TojrahTojrah

            495113




            495113












            • Are you sure? This is in Python 2. Did you run and check?

              – Austin
              Mar 23 at 6:37











            • nope, this does not work either...

              – Salman Ahmad
              Mar 23 at 7:40

















            • Are you sure? This is in Python 2. Did you run and check?

              – Austin
              Mar 23 at 6:37











            • nope, this does not work either...

              – Salman Ahmad
              Mar 23 at 7:40
















            Are you sure? This is in Python 2. Did you run and check?

            – Austin
            Mar 23 at 6:37





            Are you sure? This is in Python 2. Did you run and check?

            – Austin
            Mar 23 at 6:37













            nope, this does not work either...

            – Salman Ahmad
            Mar 23 at 7:40





            nope, this does not work either...

            – Salman Ahmad
            Mar 23 at 7:40

















            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%2f55311217%2funable-to-get-desired-result-from-if-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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript