gpt4 book ai didi

Python 输入问题?

转载 作者:太空宇宙 更新时间:2023-11-03 12:00:10 25 4
gpt4 key购买 nike

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)

所以这段代码给我带来了一些麻烦,在启动代码时一切正常,它要求我按应有的方式输入命令,如果我输入“帮助”,它就会完全按照预期执行要做的是列出所有命令,之后它应该通过 start() 命令重置并再次要求我输入命令,但是这次无论我写什么它都会激活这段代码:

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

有人可以帮我解决这个问题或者解释为什么会这样吗??

先谢谢你。

最佳答案

你必须在这里使用 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']

关于Python 输入问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51489230/

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