I need to write a program that gets user input for 2 things. One question asks f
ID: 3542509 • Letter: I
Question
I need to write a program that gets user input for 2 things. One question asks for how many items in a shopping list the user needs, prompting them to enter each item, and the other question asks how many items they actually bought, prompting them for each item. I understand how to do this part of the program.
Here is what I need help with:
Now, I need to compare the original list to the actual list so that the user receives an output of which items they still need and which items were not in the orginal list. Here is a sample of what should be shown:
How many items are you going to buy?
> 3
What is item #1?
> baking sheet
What is item #2?
> eggs
What is item #3?
> eggs
How many items did you end up buying?
> 4
What was item #1?
> eggs
What was item #2?
> cheese
What was item #3?
> spatula
What was item #4?
> cup
You still need:
baking sheet
eggs
You bought the following that were not in the original list:
cheese
cup
spatula
Notice that if the user wanted 2 packs of eggs and only bought 1, there would still be 1 under what they need to buy. Also, each result should come out in alphabetical order. Please help me with this, and thank you so much!
Explanation / Answer
#Input
items = int(raw_input("How many items you are going to buy? "))
to_buy=[]
#start for loop
for i in range(0,items):
item = raw_input("What is item #"+str(i+1)+" ?")
to_buy.append(item)
#end loop
to_buy.sort()
items2 = int(raw_input("How many items did you end up buying? "))
bought=[]
#start for loop
for i in range(0,items2):
item = raw_input("What was item #"+str(i+1)+" ?")
bought.append(item)
#end loop
bought.sort()
not_bought=[]
#start for loop
for i in range(0,items2):
x= bought[i]
if x in to_buy:
to_buy.remove(x)
else:
not_bought.append(x)
#end loop
print("You still need:")
print(" ".join(to_buy))
print(" ")
print("You bought the following that were not in the original list:")
print(" ".join(not_bought))
print(" ")