python无法让代码工作

时间:2018-04-28 22:15:02

标签: python

更新所以看起来无论我做什么或键入它要求 你想补充什么?  而不是做其他选择它只是不断问这个 如果用户键入

,它应该只询问这个问题

这是我发布的第2件事,主要原帖在这里

How to debug errors in 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())

1 个答案:

答案 0 :(得分:0)

我已更新了仍然存在缺陷的代码,并利用了list数据类型的可变性,但确实可以正常工作:

#!/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()