Python输入问题?
因此,这段代码给我带来了一些麻烦,在启动代码时,一切正常,它要求我输入一个命令,如果我输入“help”,它会完全执行它应该执行的操作,即列出所有命令,然后应该通过start()命令重置,并再次要求我输入一个命令,但是这次无论我写什么,它都会激活这段代码: 谁能帮我解决这个问题,或者解释一下为什么会发生这种情况Python输入问题?,python,python-3.x,input,Python,Python 3.x,Input,因此,这段代码给我带来了一些麻烦,在启动代码时,一切正常,它要求我输入一个命令,如果我输入“help”,它会完全执行它应该执行的操作,即列出所有命令,然后应该通过start()命令重置,并再次要求我输入一个命令,但是这次无论我写什么,它都会激活这段代码: 谁能帮我解决这个问题,或者解释一下为什么会发生这种情况 感谢您的高级支持。您必须在此处使用while循环,而且repr(i)比““+i+””更有效。: 示例输出: name_list = [] command_list = ["Add" ,"H
感谢您的高级支持。您必须在此处使用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']
您问题的解决方案:
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
您可能需要一段时间的循环somewhere@AshishAcharya什么的while循环?如果一个答案解决了你的问题,请先接受它。你的
If
语句将始终为真。这不是你想要的情况。还有repr(i)
比““+i+””更高效。
还有repr(i)
比“+i+”更高效。
['Bob']
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))
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.
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
name_list = []
command_list = ["Add" ,"Help"]
def start():
inpt = input("Hello please type in a command : ")
if (inpt == "help" or inpt == "Help"):
print("Here are the following commands for this program : ")
print(command_list)
start()
elif (inpt == "Add" or inpt == "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
yn = input("Do you want to add another person? ")
if yn in ['y','Y','yes','Yes','YES']:
start()
else:
print ("Quitting!! all the people entered are")
print(name_list)
return (None)
else:
print("Please type 'help' for help")
start()