在Python中使用原始输入收集输入

在Python中使用原始输入收集输入,python,input,python-2.x,Python,Input,Python 2.x,因此,我尝试使用raw\u input获取程序的输入,我有: def Input1(time): userInput = raw_input() print ("Please choose either morning or night: ") if userInput != "morning" or "night": print ("Invalid entry. Select again") Input1(time) if

因此,我尝试使用
raw\u input
获取程序的输入,我有:

def Input1(time):

    userInput = raw_input()
    print ("Please choose either morning or night: ")

    if userInput != "morning" or "night":
        print ("Invalid entry. Select again")
        Input1(time)

    if userInput in "morning" or "night":
        Input2(year)
第二个
if
语句提示它进行更多编程

当我试着运行这个程序时,它会运行所有的东西,但它不会要求用户输入任何东西。有什么想法吗


虽然没有显示,但在
def Input1(time)下,所有内容都是选项卡式的:

查看操作符的顺序。首先,您要求用户输入信息;然后,打印“请选择”提示。因此,在用户输入值之前不会出现此提示。正确的方法:

userInput = raw_input("Please choose either morning or night:")

另外,据我所知,
if
语句中的条件不正确。请参阅和或运算符中有关
的Python文档。

查看运算符序列。首先,您要求用户输入信息;然后,打印“请选择”提示。因此,在用户输入值之前不会出现此提示。正确的方法:

userInput = raw_input("Please choose either morning or night:")

另外,据我所知,
if
语句中的条件不正确。请参阅
和或运算符中有关
的Python文档。

您可以在
原始输入
调用中输入提示:

userInput = raw_input("Please choose either morning or night: ")

对于此逻辑:
if userInput!=“早上”或“晚上”
如果用户输入,您实际上想要
“早晨”和用户输入!=“晚上”
。另一种编写方法是'if userInput not in('morning','night')。类似的逻辑也适用于您的第二个if语句。

您可以在
原始输入
调用中输入提示:

userInput = raw_input("Please choose either morning or night: ")

对于此逻辑:
if userInput!=“早上”或“晚上”
如果用户输入,您实际上想要
“早晨”和用户输入!=“晚上”
。另一种编写方法是'if userInput not in('morning','night')。类似的逻辑也适用于第二条if语句。

在运行代码时,您是否真的调用了Input1(time)?如果您不调用它,它将不会运行。

您运行代码时是否真的调用了Input1(时间)?如果不调用它,它将不会运行。

raw\u input()
已重命名为
input()

raw\u input()
重命名为
input()


中可以看出,由于许多原因,此代码不好:

def Input1(time):
函数名不应以大写字母开头

userInput = raw_input()
print ("Please choose either morning or night: ")
只需写:userinput=raw\u input(“请选择早晨或晚上:”)

这相当于:

if userInput != "morning" or True:
这永远是真的

    print ("Invalid entry. Select again")
    Input1(time)
在这里,你做一个递归调用,再次询问。。。但是如果没有任何返回,这意味着将多次调用下面的函数(实际上没有,因为您无法退出该函数)

同样的错误,应该是:

if userInput in ["morning", "night"]:


此代码不好的原因有很多:

def Input1(time):
函数名不应以大写字母开头

userInput = raw_input()
print ("Please choose either morning or night: ")
只需写:userinput=raw\u input(“请选择早晨或晚上:”)

这相当于:

if userInput != "morning" or True:
这永远是真的

    print ("Invalid entry. Select again")
    Input1(time)
在这里,你做一个递归调用,再次询问。。。但是如果没有任何返回,这意味着将多次调用下面的函数(实际上没有,因为您无法退出该函数)

同样的错误,应该是:

if userInput in ["morning", "night"]:


您是否正在调用
Input1
?要缩进,您需要在每行代码前面使用4个空格,因为这是向SO帖子添加代码的方式。
如果userInput in“morning”或“night”:
应该是
如果userInput in['morning',night]:
如果userInput='morning'或userInput='night':
。现在,如果userInput in'morning'或'night'==True:
,则该行相当于
,这当然总是正确的。(PS:
'mor'in'morning'==True
)你到底在呼叫
Input1
?要缩进,您需要在每行代码前面使用4个空格,因为这是向SO帖子添加代码的方式。
如果userInput in“morning”或“night”:
应该是
如果userInput in['morning',night]:
如果userInput='morning'或userInput='night':
。现在,如果userInput in'morning'或'night'==True:
,则该行相当于
,这当然总是正确的。(PS:
'mor'在'morning'中==True
)。。。在Python3中。如果他使用Python3和
raw\u input
,他的代码将抛出一个错误。。。。在Python3中。如果他使用Python3和
raw\u输入
,他的代码将抛出一个错误。