如果python中的语句不起作用

时间:2017-01-18 05:08:38

标签: python

我最近开始学习编码。在我的第一次任务中,我被要求制作一个程序,允许用户选择名称,然后选择随机名称。这就是我想出的:

def main():
  name = ["josh", "omar", "shatil", "cathrin"]
  while True:
    print (name)
    continueProgram = input("would you like to continue")
    if continueProgram.lower() =="yes":
      print(changeList(name))
    else:
      break
def changeList(items):
  changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
  if changeList.lower() == "a":
    appendList = input("what name would you like to add to the list?: ") items.append(appendList)
  if changeList.lower() =="r":
    removeList = input ("what name would you liket to remove from the list?: ")items.remove(removeList)
    return items
main()

最后2个if语句不起作用。

1 个答案:

答案 0 :(得分:0)

你的缩进看起来很可疑。

而不是:

def changeList(items):
  changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
  if changeList.lower() == "a":
    appendList = input("what name would you like to add to the list?: ") items.append(appendList)
  if changeList.lower() =="r":
    removeList = input ("what name would you liket to remove from the list?: ")items.remove(removeList)
    return items

试试这个:

def changeList(items):
    changeList= input("Enter 'a' to append list, 'r' to remove an item from the list: ")
    if changeList.lower() == "a":
        appendList = input("what name would you like to add to the list?: ") 
        items.append(appendList)
    if changeList.lower() =="r":
        removeList = input ("what name would you liket to remove from the list?: ")
        items.remove(removeList)
    return items