Skip to main content
 首页 » 编程设计

python之在 Python 中使用 raw_input 收集输入

2025年05月04日7jiqing9006

所以我正在尝试使用 raw_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):

下标记

请您参考如下方法:

这段代码不好的原因有很多:

def Input1(time): 

函数名称不应以大写字母开头。

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

简单写:userInuput = raw_input("请选择早上或晚上:")

if userInput != "morning" or "night": 

这相当于:

if userInput != "morning" or True: 

这总是正确的...

    print ("Invalid entry. Select again") 
    Input1(time) 

在这里,你做了一个递归调用,再次询问......但是没有任何返回,这意味着下面将被调用很多次(实际上没有,因为你不能退出那个函数)。

if userInput in "morning" or "night": 
    Input2(year) 

同样的错误,应该是:

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

或

if userInput == "morning" or userInput == "night":