简体   繁体   English

无法从python中的“ if”获得所需结果

[英]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. 除了当用户在输入中输入一些字母(字符串)时,一切都完成了,控制台给出了一条错误声明,表明我已经在if语句中包括了代码。

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. 我刚刚开始学习python。 And this is my very first code in python. 这是我在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. 您不必使用month = string(month),因为输入仅是字符串形式。 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 . 在Python 2中,由于当时必须理解的原因, input()函数试图评估输入为Python表达式的内容 Thus, when you type jan into an input() prompt, it tries to find the variable jan . 因此,当您在input()提示符下键入jan时,它将尝试查找变量jan This is not what you want in this (or pretty much any other*) case; 在这种情况下(或几乎所有其他*),这不是您想要的; you should use raw_input() instead. 您应该改用raw_input()

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() . 请注意,在Python 3中(这是从2020年开始唯一受支持的版本input() ,将删除Python 2的input()函数,并将raw_input()函数重命名为just input()


* It might seem that when you want a number, input() makes sense; *似乎当您想要一个数字时, input()很有意义; when the user types 2 to an input() prompt, you get an int , not a str . 当用户键入2input()提示,你会得到一个int ,而不是一个str But there are massive security problems any time you run eval , which input() does, so you should still use int(raw_input()) instead. 但是,每当您运行evalinput()都会遇到大量安全问题,因此您仍然应该使用int(raw_input())代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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