简体   繁体   中英

Unable to get desired result from “ if ” in 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:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.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:\Python27\lib\site-packages\IPython\kernel\zmq\ipkernel.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"

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM