user3293865
user3293865

Reputation: 3

Python: Grocery list/ comparing two sets of user input

The objective of this program is to:

i.e

"Here are the items you still need to buy: bread eggs eggs ham

Here are the unnecessary items you bought: chips turkey"

Here is my code thus far:

count = 1
count2 = 1

# of items on list
item_numN = int(raw_input("Please enter the number of items on your grocery list.\n"))

for i in range (0,item_numN):
    item_list = str(raw_input("What is the item #" + str(count) + " on your list?\n"))
    count = count + 1

# of items bought
item_numB = int(raw_input("Please enter the number of items you bought.\n"))

for i in range (0,item_numB):
    item_bought = str(raw_input("What is the item #" + str(count2) + " that you bought?\n"))
    count2 = count2 + 1

I can't quite figure out how to read the two separate sets of input and compare them. Any help would be greatly appreciated.

Upvotes: 0

Views: 967

Answers (3)

user3294259
user3294259

Reputation: 1

Lists and Sets in Python don't have preset size restriction, they are so to say endless. So you don't really need to ask how many items user bought. You can prompt something like this:

items_bought=[]
items_to_buy=[]
item_count=1
while item != 'Done' or item != 'done':
    item = str(raw_input("Add item #%i or type done if you are finished"%item_count))
    items_bought.append(item)
    item_count+=1
item_count-=1 #due to last iteration, when user decides, that this is it, item count will be by 1 higher

Same could be done for to buy list. Note sets only contain 1 instance for each item, lists can contain many repeatable items. Reference to documentation on Python data sructs: Python Data Structures

Upd: To understand iterable data structs here are some hints: You can call item of a list like so: list_name[item_number] Numeration starts with 0, so items in the list would be: list_name[0], list_name1 ... list_name[n-1] with total of n items. Slicing. You can slice iterables in python like so: list_name[start_position:end_position:step]. You can slice from the end of the list to the beginning too. Here is the link to stack overflow thread about slicing: Slicing in Python

Good luck, Python is a great language to learn, you will certainly enjoy it.

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32449

You can use sets to find the differences.

Trying to keep as much as possible of your original code:

item_numN = int(raw_input("Please enter the number of items on your grocery list.\n"))
item_list = [str(raw_input("What is the item #" + str(count + 1) + " on your list?\n")) for count in range(item_numN)]

item_numB = int(raw_input("Please enter the number of items you bought.\n"))
item_bought = [str(raw_input("What is the item #" + str(count + 1) + " that you bought?\n")) for count in range(item_numB)]

items_needed = set(item_list) - set(item_bought)
print 'You still need {}.'.format(', '.join(items_needed))

Here a sample session:

Please enter the number of items on your grocery list.
3
What is the item #1 on your list?
apples
What is the item #2 on your list?
pears
What is the item #3 on your list?
beer
Please enter the number of items you bought.
4
What is the item #1 that you bought?
beer
What is the item #2 that you bought?
paper
What is the item #3 that you bought?
pencil
What is the item #4 that you bought?
roses
You still need apples, pears.

In analogy, the items bought without being on the list, would be set(item_bought) - set(item_list).

Upvotes: 1

Chobeat
Chobeat

Reputation: 3525

Is this an homework?

The first thing you're missing is that you need to create empty lists and then append to them

Something like

item_bought=[]    
for i in range (0,item_numB):
        item_bought.append(str(raw_input("What is the item #" + str(count2) + " that you bought?\n")))

Then you will be able to compare them sorting and cycling through them.

Upvotes: 0

Related Questions