猿问

具有多个 IF 条件的 While 循环

我写了一个程序,从用户那里接收一个代表购物清单的字符串。该程序要求用户输入一个介于 1 和 9 之间的数字。根据收到的数字,执行以下操作之一: 在进行用户选择后,用户返回到主菜单,直到他们选择数字 9 退出。语法正确,但程序未打印所需内容。如何解决?


def shopping_list(my_str):

    my_list = my_str.split(",")

    i = input("Please choose a number between 1 and 9: ")

    while i in range(1, 10):

        if i == 1:

            print("My shopping list:", my_list)

            continue

        elif i == 2:

            print("The number of items in my shopping list:", len(my_list))

            continue

        elif i == 3:

            product = input("Please enter a product name: ")

            if product in my_list:

                print("This product is in the shopping list.")

            else:

                print("This item is not in the shopping list.")

            continue

        elif i == 4:

            product = input("Please enter a product name: ")

            print("The item", product, "shows", my_list.count(product), "in the list")

            continue

        elif i == 5:

            product = input("Please enter a product name: ")

            new_list = my_list.remove(product)

            print("The item", product, "remove from the list. The new list is", new_list)

            continue

        elif i == 6:

            product = input("Please enter a product name: ")

            my_list += product

            print("The item", product, " add to the list. The new list is", my_list)

            continue

        elif i == 7:

            new_list = []

            for product in my_list:

                if len(product) < 3 or not(product.isalpha()):

                    new_list += product

            continue

        elif i == 8:

            print(list(set(my_list)))

            continue

        else:

            break

shopping_list("Milk,Cottage,Tomatoes")


心有法竹
浏览 305回答 2
2回答

哆啦的时光机

您再也不会询问用户,因此循环会无限地执行用户给出的第一个选择。同时删除该continue语句,您不需要它们,因为所有代码都在其中,elif它还允许您在循环结束时询问用户一个新的选择。将输入转换为int,你将无法进入循环def shopping_list(my_str):&nbsp; &nbsp; my_list = my_str.split(",")&nbsp; &nbsp; i = int(input("Please choose a number between 1 and 9: "))&nbsp; &nbsp; while i in range(1, 10):&nbsp; &nbsp; &nbsp; &nbsp; if i == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("My shopping list:", my_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; elif i == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("The number of items in my shopping list:", len(my_list))&nbsp; &nbsp; &nbsp; &nbsp; elif i == 3:&nbsp; &nbsp; &nbsp; &nbsp; # ...&nbsp; &nbsp; &nbsp; &nbsp; elif i == 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(list(set(my_list)))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; i = int(input("Please choose a number between 1 and 9: "))最终完整代码现在更正一下mode 5: 返回的remove是None,修改就地如此做elif i == 5:&nbsp; &nbsp; product = input("Please enter a product name: ")&nbsp; &nbsp; my_list.remove(product)&nbsp; &nbsp; print("The item", product, "remove from the list. The new list is", my_list)mode 6操作员在列表上+=执行一个操作extend,因此它将添加所有字符,append改为执行elif i == 6:&nbsp; &nbsp; product = input("Please enter a product name: ")&nbsp; &nbsp; my_list.append(product)&nbsp; &nbsp; print("The item", product, " add to the list. The new list is", my_list)mode 7如果您忘记创建一个作为主要过滤器过滤器的新列表,那将毫无用处。另外我会说你删除小于 3 或包含非 alpha 的项目,在这里你保留它们。最后使用appendelif i == 7:&nbsp; &nbsp; new_list = []&nbsp; &nbsp; for product in my_list:&nbsp; &nbsp; &nbsp; &nbsp; if len(product) >= 3 and product.isalpha():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_list.append(product)&nbsp; &nbsp; my_list = list(new_list)或者只是使用列表理解elif i == 7:&nbsp; &nbsp; my_list = [p for p in my_list if len(p) >= 3 and p.isalpha()]

HUH函数

我会这样做:(由于代码太多,删除了一些 elifs。)while True:&nbsp; &nbsp; i = int(input("Please choose a number between 1 and 9: "))&nbsp; &nbsp; if i == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("My shopping list:", my_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; elif i == 8:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(list(set(my_list)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; elif i == 9:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Invalid Number! Try again.")这是你想要的吗?我不太明白你的要求。
随时随地看视频慕课网APP

相关分类

Python
我要回答