Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON PROGRAMMING AND WHILE LOOPS (DO NOT USE FOR LOOPS) This program all

ID: 3587265 • Letter: U

Question

USING PYTHON PROGRAMMING AND WHILE LOOPS (DO NOT USE FOR LOOPS)

This program allows the user to create a grocery list, with

the user being

allowed to

enter

items indefinitely, stopping only when they en

ter the sent

inel

value “STOP”.

If the user tries to enter an item already

in the list

, the

program should print out an error message and not add the duplicate to the

list.

You may NOT use the membership

in

operator for this

problem.

After

the user indicates they are done, their complete grocery list

should be

printed back out to them,

i

n the same order it was entered,

along with

the

total number of items on the

ir

list.

The program

must

m

ake

use of a

list to accomplish

t

hese tasks

!

SAMPLE OUTPUT:

Please enter a grocery item ('STOP' to exit):

STOP

There are 0 groceries in your list:

bash

-

4.1$ python hw4_part7.py

Please enter a grocery item ('STOP' to exit):

dog food

Please enter a grocery item ('STOP' to exit):

b

utter

Please enter a grocery item ('STOP' to exit):

dog

f

ood

Error: The item dog food is already in the list.

Please enter a grocery item ('STOP' to exit):

c

heese

Please enter a grocery item ('STOP' to exit):

dog food

Error: The item d

og food is already in the list.

Please enter a grocery item ('STOP' to exit):

steak

Please enter a grocery item ('STOP' to e

xit):

ice cream

Please enter a grocery item ('STOP' to exit):

s

teak

Error: The item

s

teak

is already in the list.

Please enter a grocery item ('STOP' to exit):

STOP

There are 5 groceries in your list:

dog food

butter

c

heese

steak

ice cream

Explanation / Answer

list=[]
item = ""
while item != 'STOP':
   item = input("Please enter a grocery item('STOP' to exit):")
   if item != 'STOP':
     count = 0
     found = 0
     while count < len(list) and found == 0 :
       if list[count] == item:
          found = 1
         
       count = count + 1
     if found == 1:
      print(item, " already in the list.")
     else:
      list.append(item)
  

count = 0
print("There are ",len(list), " groceries in your list:");
while count < len(list) :
   print(list[count])
   count = count + 1