首页 > 解决方案 > python 无法让代码正常工作

问题描述

更新所以看起来无论我做什么或输入它都会询问您要添加什么?而不是做其他选项它只是一直问这个它应该只在用户输入a时才问这个

这是我遇到的两个问题,主要的原始帖子在这里

如何调试 Python 中的错误?

但我遇到的更大问题是我无法让我的代码的下半部分工作
它只是忽略给它的输入并继续将它们添加到列表中

#.....
      elif user == "d" :
         listdelete = input("what item do you want deleted")
         list.remove (listdelete)
      elif user == "p" : # prints the list 
       print (list)
      elif user == "x" :  # exits the program 
       print ("good bye")
print ("!!!!!!that is not a choice!!!!!!")
   #right now everything is being called through a single procedure
print (option())

并且在底部这里将是整个代码抱歉将整个代码粘贴在底部但我认为如果我显示整个代码然后该部分不起作用则很容易理解整个问题因为它的全部在一个程序下

list = []
def option() :
   print ("(a) -to add item ")
   print ("(d) -to delete item")
   print ("(p) -to see items on your list")
   print ("(x) -to exit ")
#above just prints out user options with a input after
   user = input("please select a choice")
   if user == "a" :
      item = input("what would you like to add? ")
      list.append (item)
   # next im going to ask if they would like to add another item 
   more = input(" would you like to add something else? yes or no")
   while more == "yes":
      if more == "yes" :
         item = input("what else would you like to add? type b to back ")
      if item == "b" :
            print (option())
      else :
            list.append(item)
      elif more == "no" :
         print (option()) 
#.....
      elif user == "d" :
         listdelete = input("what item do you want deleted")
         list.remove (listdelete)
      elif user == "p" : # prints the list 
       print (list)
      elif user == "x" :  # exits the program 
       print ("good bye")
print ("!!!!!!that is not a choice!!!!!!")
   #right now everything is being called through a single procedure
print (option())

标签: python

解决方案


我已经更新了您的代码,它仍然存在缺陷并利用了列表数据类型的可变性,但确实可以按您的预期工作:

#!/usr/bin/env python


def AddElement(collection):
    elementToAdd = input("What would you like to add: ")
    collection.append(elementToAdd)

    return collection


def DeleteElement(collection):
    print("You have the following items:", collection)
    elementToDelete = input("What item do you want deleted: ")
    for elements in collection:
        if elements == elementToDelete:
            collection.remove(elements)
    print("You have the following items left:", collection)

    return collection

def WriteMenu():
    print("(a) -to add item")
    print("(d) -to delete item")
    print("(p) -to see items on your lst1")
    print("(x) -to exit")

    return True

def option():
    WriteMenu()
    UserInput = input("Please select a choice: ")
    while UserInput:
        if UserInput == "a":
            AddElement(collection)
        elif UserInput == "d":
            DeleteElement(collection)
        elif UserInput == "p":
            print(collection)
        elif UserInput == "x":
            print("Good bye!")
            break
        WriteMenu()
        UserInput = input("Please select a choice: ")
    else:
        print(collection)

#right now everything is being called through a single procedure
collection = []
option()

推荐阅读