0

I wrote a program that receives from the user one string representing a shopping list. The program asks the user to enter a number between 1 and 9. Depending on the number received, do one of the following: And after making a user selection, the user returns to the main menu until they select number 9 to exit. The syntax is correct but the program does not print what is needed. How to fix it?

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")
6
  • 1
    Look at where you are taking in user input. Commented May 13, 2020 at 21:17
  • i never changes inside the loop, so that's an infinite loop Commented May 13, 2020 at 21:17
  • Use continue when you want to avoid code that comes next, here there is just elif so no other code will be executed : remove all the continue statement Commented May 13, 2020 at 21:19
  • input() returns a string value, and therefore while i in range(1, 10) will never execute. Commented May 13, 2020 at 21:20
  • replace your input() inside your loop to reiterate the request with while true. Commented May 13, 2020 at 21:22

2 Answers 2

1
  • You never ask again the user, so the loop goes infite doing the first choice given by the user.
  • Also remove the continue statement, you don't need them as all code is in elif also it'll allow you to ask the user a new choice at the end of the loop.
  • convert the input to int, you won't be able to enter the loop without
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: "))

Final Full Code


Now corrections about

  • mode 5 : what is returned by remove is None, the modification is in-placed so do

    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 the operator += does an extend on the list so it'll add all chars, do append instead

    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 creating a new list that is a filter of the main one is useless if you forget it. Also I'd say you remove the items that are smaller than 3 or contains non-alpha, here you keep them. Finally use 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)
    

    or just use a list comprehension

    elif i == 7:
        my_list = [p for p in my_list if len(p) >= 3 and p.isalpha()]
    
Sign up to request clarification or add additional context in comments.

4 Comments

@JohnGordon thanks, I was preparing the fix of the list error too
@Shlomit did you accept the answer that explains just 25% of the problems ?
@JohnGordon thank you so much, it was very helpful! Why when i == 5 can't I add an item that already exists in the list? Why when i == 8 and the same item appears about 3 times does it not make it distinct and returns it twice? set Shouldn't remove duplicates?
@Shlomit Me is azro ^^ you should type them exactly the same , now why did you accept the other answer that does not tell you about your other problems ?
0

I would do this: (Cut out some elifs due to too much code.)


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.")

Is this what you want? I'm not quite getting what you're asking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.