Python输入问题?

时间:2018-07-24 01:33:54

标签: python python-3.x input

name_list = []
command_list = ["Add" ,"Help"]

def start():
 input("Hello please type in a command : ")

start()

if (input == "help" or "Help"):
    print("Here are the following commands for this program : ")
    i = 0
    for i in command_list :
       print("'" + i + "'")
    start()


if (input == "Add" or "add" ):
 name = input("Insert name please : ")
 print("Welcome " + name)
 name_list.append(name)

所以这段代码给我带来了一些麻烦,启动代码后一切正常,它要求我按原样键入命令,如果我键入“ help”,它会按照预期运行要做的是列出所有命令,然后通过start()命令将其重置,然后再次要求我输入命令,但是这次无论我写什么,都会激活此代码块:

 if (input == "Add" or "add" ):
 name = input("Insert name please : ")
 print("Welcome " + name)
 name_list.append(name) 

有人可以帮我解决这个问题,或解释为什么会发生这种情况吗?

谢谢你。

4 个答案:

答案 0 :(得分:0)

您必须在此处使用while循环,而且repr(i)"'" + i + "'"更有效:

name_list = []
command_list = ["Add" ,"Help"]

def start():
    return input("Hello please type in a command : ")

a=start()

while a.lower()=='help':
    print("Here are the following commands for this program : ")
    i = 0
    for i in command_list :
       print(repr(i))
    a=start()


if a.lower()=='add':
 name = input("Insert name please : ")
 print("Welcome " + name)
 name_list.append(name)
else:
 print('invalid input')

示例输出:

Hello please type in a command : help
Here are the following commands for this program : 
'Add'
'Help'
Hello please type in a command : Help
Here are the following commands for this program : 
'Add'
'Help'
Hello please type in a command : Add
Insert name please : Bob
Welcome Bob

还有:

print(name_list)

返回:

['Bob']

答案 1 :(得分:0)

您的代码有几个问题。这是一个有效的版本。

name_list = []
command_list = ["Add", "Help"]


def start():
    return input("Hello please type in a command : ")


name = ""

while not name:
    command = start()

    if (command.lower() == "add"):
        name = input("Insert name please : ")
        print("Welcome " + name)
        name_list.append(name)

    # printing help for everything except add
    else:
        print("Here are the following commands for this program : ")
        i = 0
        for i in command_list:
            print(repr(i))

答案 2 :(得分:0)

if (input == "help" or "Help"):
    print("Here are the following commands for this program : ")
    i = 0
    for i in command_list :
       print("'" + i + "'")
    start()  # <--- your call to start() function does not reset where the interpreter left off reading you code.

Python解释器仅读取您的每一行,并对其进行处理。在您的if语句中,行start()被调用,它要求用户输入并返回None。然后,如果此if语句完成,那么下一行将如您所述:

if (input == "Add" or "add" ):
 name = input("Insert name please : ")
 print("Welcome " + name)
 name_list.append(name)

您的问题的解决方案:

name_list = []
command_list = ["Add" ,"Help"]

def start():
 input("Hello please type in a command : ")
while True:
   start()

   if (input == "help" or "Help"):
       print("Here are the following commands for this program : ")
       i = 0
       for i in command_list :
          repr(i)

   if (input == "Add" or "add" ):
    name = input("Insert name please : ")
    print("Welcome " + name)
    name_list.append(name)

   if input == "something for break condition":
       break

答案 3 :(得分:0)

//span[text() = ' posts']/span