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;
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
add a comment |
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
3
Useraw_input
instead ofinput
.month = str(month)
line is not required asmonth
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
add a comment |
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
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
python
asked Mar 23 at 6:28
Salman AhmadSalman Ahmad
63
63
3
Useraw_input
instead ofinput
.month = str(month)
line is not required asmonth
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
add a comment |
3
Useraw_input
instead ofinput
.month = str(month)
line is not required asmonth
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
You need not use month=string(month) as the input is in string form only. Remove the line and code should work
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
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 23 at 8:39
Daniel HDaniel H
5,45211831
5,45211831
add a comment |
add a comment |
You need not use month=string(month) as the input is in string form only. Remove the line and code should work
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
add a comment |
You need not use month=string(month) as the input is in string form only. Remove the line and code should work
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
add a comment |
You need not use month=string(month) as the input is in string form only. Remove the line and code should work
You need not use month=string(month) as the input is in string form only. Remove the line and code should work
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
add a comment |
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
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%2f55311217%2funable-to-get-desired-result-from-if-in-python%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
3
Use
raw_input
instead ofinput
.month = str(month)
line is not required asmonth
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