我试图将grab
命令限制为一次只允许列表中的4个项目以及drop
和edit
命令。如果用户输入任意数量的无效数字项,我会尝试添加一条说invalid
的邮件。
def grab(item_list):
item = input("Name: ")
item_list.append(item)
print(item + " was added.\n")
def edit(item_list):
pos = int(input("Number: "))
new_value = input("Updated name: ")
item_list[pos-1] = new_value
print("%s was updated" % (new_value))
def drop(item_list):
number = int(input("Number: "))
item = item_list.pop(number-1)
print(item + " was dropped.\n")
print()
def main():
item_list = ["wooden staff","wizard hat","cloth shoes"]
答案 0 :(得分:0)
def grab(item_list):
if len(item_list)==4:
print('Cannot grab more than 4. Drop another item and try again')
return
item = input("Name: ")
item_list.append(item)
print(item + " was added.\n")
def edit(item_list):
pos = int(input("Number: "))
if not (pos-1)in range(len(item_list)):
print(str(pos) + ' not in list, try again')
edit(item_list)
return
new_value = input("Updated name: ")
item_list[pos-1] = new_value
print("Updated item %d with new value %s" % (pos, new_value))
def drop(item_list):
number = int(input("Number: "))
if not (number-1) in range(len(item_list)):
print(str(number) + ' not in list, try again')
drop(item_list)
return
item = item_list.pop(number-1)
print(item + " was dropped.\n")
print()