0

The objective of this program is to:

  • prompt user for number of items needed

  • read what items are needed

  • prompt user for number of items bought

  • read what items have been bought

  • Lastly, compare the two lists to see which items have been bought vs what unnecessary items have been bought.

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.

3 Answers3

1

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

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • Comparing two sets of input: make sets. This is probably the most concise thing I've actually been able to find. Thank you. So out of curiosity, is this still a viable option if I were to create more sets? Instead of comparing two lists, let's say I were to compare five? – user3293865 Feb 10 '14 at 18:04
  • @user3293865 Sure just merge, intersect and subtract them as needed. – Hyperboreus Feb 10 '14 at 19:03
0

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.

Chobeat
  • 3,445
  • 6
  • 41
  • 59
  • Doesn't something like `for i in range(count): someList.append(...)` strike you as odd in python? – Hyperboreus Feb 10 '14 at 17:35
  • 2
    absolutely but this is clearly homework and i tried not to go outside his knowledge and do the exercise in a way he could easily understand. – Chobeat Feb 10 '14 at 17:38
  • Oh yes this is homework. Unfortunately for me, it's an online course and about 98% self-taught. The "professor" has a few videos up vaguely pertaining to a few of the assignments and the only other resources offered are, you guessed it, student tutors. Basically, I have to find resources on the internet, or adjust my schedule to make it to sessions only to be taught by other students. – user3293865 Feb 10 '14 at 17:48
  • And to put a bit of perspective on it: those directions I included in my post were written by myself. The original instructions aren't as clear. – user3293865 Feb 10 '14 at 17:55
0

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.

Community
  • 1
  • 1