gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:02 24 4
gpt4 key购买 nike

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

关于python - 在 Python 中使用 raw_input 收集输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com