为了账号安全,请及时绑定邮箱和手机立即绑定

具有多个 IF 条件的 While 循环

具有多个 IF 条件的 While 循环

心有法竹 2022-11-18 18:16:10
我写了一个程序,从用户那里接收一个代表购物清单的字符串。该程序要求用户输入一个介于 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:            breakshopping_list("Milk,Cottage,Tomatoes")
查看完整描述

2 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

您再也不会询问用户,因此循环会无限地执行用户给出的第一个选择。

同时删除该continue语句,您不需要它们,因为所有代码都在其中,elif它还允许您在循环结束时询问用户一个新的选择。

将输入转换为int,你将无法进入循环

def shopping_list(my_str):

    my_list = my_str.split(",")

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

    while i in range(1, 10):

        if i == 1:

            print("My shopping list:", my_list)                

        elif i == 2:

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

        elif i == 3:

        # ...

        elif i == 8:

            print(list(set(my_list)))

        else:

            break

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

最终完整代码

现在更正一下


mode 5: 返回的remove是None,修改就地如此做


elif i == 5:

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

    my_list.remove(product)

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

mode 6操作员在列表上+=执行一个操作extend,因此它将添加所有字符,append改为执行


elif i == 6:

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

    my_list.append(product)

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

mode 7如果您忘记创建一个作为主要过滤器过滤器的新列表,那将毫无用处。另外我会说你删除小于 3 或包含非 alpha 的项目,在这里你保留它们。最后使用append


elif i == 7:

    new_list = []

    for product in my_list:

        if len(product) >= 3 and product.isalpha():

            new_list.append(product)

    my_list = list(new_list)

或者只是使用列表理解


elif i == 7:

    my_list = [p for p in my_list if len(p) >= 3 and p.isalpha()]


查看完整回答
反对 回复 2022-11-18
?
HUH函数

TA贡献1836条经验 获得超4个赞

我会这样做:(由于代码太多,删除了一些 elifs。)



while True:

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


    if i == 1:

            print("My shopping list:", my_list)

            continue

        elif i == 8:

            print(list(set(my_list)))

            continue

        elif i == 9:

            break

        else:

            print("Invalid Number! Try again.")


这是你想要的吗?我不太明白你的要求。


查看完整回答
反对 回复 2022-11-18
  • 2 回答
  • 0 关注
  • 304 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号