1

Hi I am bit new to python coding and I have prepared a pseudo code but I can't write my python code properly. This are my pseudo code:

Input(Item)
Item = Item.split()    
numberOfItem = count(Item)
until numberOfItem == 2:
   output("Please select two Item")
   input(Item)
itemCostDic = {"wood":200, "paper":100, "pen":10, "eraser":5}
specificItemCost = {}
for value in Item:
   specificItemCost[value] = itemCostDic[value]
totalItemCost = sum(specificItemCost.value)
print(totalItemCost)

I am not sure how to loop the "until" in my python codes.

van neilsen
  • 547
  • 8
  • 21
Dev Patel
  • 35
  • 1
  • 8
  • `while numberOfItem < 2:` - this wont run the loop body if you already have 2 items in it - its used when you add things to your list and want to stop at 2. you need to somehow modify the value you check in your condition inside your loop (or directly at the while - lvl) or you have a endless loop – Patrick Artner Jun 23 '18 at 07:06
  • Use a while or for loop and break out if necessary. – Jan Jun 23 '18 at 07:06
  • 2
    and please read [Ask user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response/23294659) – Patrick Artner Jun 23 '18 at 07:10

2 Answers2

2

'Until' can be achieved with a while not equal to loop in python:

while numberOfItem != 2:
    ...

But you will need to incorporate the changing value of numberOfItem into the loop itself in order to have it break at some point:

#initialise variable
nuberOfItem=0

while numberOfItem != 2:
    Item = input("Please select two items: ").split()
    numberOfItem = len(Item)
iacob
  • 20,084
  • 6
  • 92
  • 119
0

while numberOfItem != 2: will loop until you got 2 items. This wont run the loop body if you initially have 2 items in it - this kind of check is used when you add/remove things to your list inside the loop body and want to stop at exactly 2 items in list.

You need to somehow modify the value you check in your condition inside your loop (or directly do a while len(yourList) != 2: dynamic checking at the while - lvl) or you have a endless loop.


You refine your code using your dict to verify only valid items are given. You can store the inputs into a second dict along with amounts and sum them after all inputs are done, something along:

(The code incorporates Asking the user for input until they give a valid response methods to verify user input)

itemCostDic = {"wood":200, "paper":100, "pen":10, "eraser":5}
print("Inventory:")
for k,v in itemCostDic.items():
    print( "   - {} costs {}".format(k,v))
print("Buy two:")
shoppingDic = {}
while len(shoppingDic) != 2:
   # item input and validation
   item = input("Item:").lower()
   if item not in itemCostDic:   # check if we have the item in stock
          print("Not in stock.")
          continue # jumps back to top of while
   if item in shoppingDic:       # check if already bought, cant buy twice
          print("You bought all up. No more in stock.")
          continue # jumps back to top of while

   # amount input and validation
   amount = input("Amount:")
   try:
          a = int(amount)         # is it a number? 
   except ValueError:             
          print("Not a number.")  
          continue # start over with item input, maybe next time user is wiser

   # add what was bought to the cart
   shoppingDic[item] = a

s = 0
print("Bought:")
for k,v in shoppingDic.items():
    print( "   - {} * {} = {}".format(k,v, itemCostDic[k]*v))
    s += itemCostDic[k]*v
print("Total: {:>12}".format( s)) 

Output:

Inventory:
   - wood costs 200
   - paper costs 100
   - pen costs 10
   - eraser costs 5
Buy two:
Item:socks
Not in stock.
Item:paper
Amount:5
Item:paper
You bought all up. No more in stock.
Item:pen
Amount:k
Not a number.
Item:pen
Amount:10
Bought:
   - paper * 5 = 500
   - pen * 10 = 100
Total:          600

No amounts:

itemCostDic = {"wood":200, "paper":100, "pen":10, "eraser":5}
print("Inventory:")
for k,v in itemCostDic.items():
    print( "   - {} costs {}".format(k,v))
print("Buy two:")
shoppingCart = set() # use a list if you can shop for 2 times paper
while len(shoppingCart) != 2:
   # item input and validation
   item = input("Item:").lower()
   if item not in itemCostDic:   # check if we have the item in stock
          print("Not in stock.")
          continue # jumps back to top of while
   if item in shoppingCart:       # check if already bought, cant buy twice
          print("You bought all up. No more in stock.")
          continue # jumps back to top of while

   # add what was bought to the cart
   shoppingCart.add(item)

s = 0
print("Sum of shopped items: {:>6}".format( sum ( itemCostDic[i] for i in shoppingCart) ))

Output:

Inventory:
   - wood costs 200
   - paper costs 100
   - pen costs 10
   - eraser costs 5
Buy two:
Item:socks
Not in stock.
Item:paper
Item:paper
You bought all up. No more in stock.
Item:wood
Sum of shopped items:    300
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • your codes work well but what if i want to have my input on the same line – Dev Patel Jun 23 '18 at 07:37
  • @DevPatel `item,amount = input("Item and amount, space between").split()` - this will crash if only an item is given, you would have to add `try: except ValueError:` around that as well to catch errors introduced by ppl inputing _only_ an item or number. then proceed with the "is item in dict check" and "is number really an int". – Patrick Artner Jun 23 '18 at 07:49
  • I am not looking for the amount in this case, all i am looking for is when the user inputs two items i shoud dispaly the total cost between the two products written – Dev Patel Jun 23 '18 at 07:52
  • @DevPatel try go understand what the code does, you can kill the "amount" stuff (input + validation) change the shopping cart to a simple list and simplify the output and sum calculation to suit your needs. If you want to get both items in one line you have to split them and check if _both_ are in your `itemCostDic`. Modify the last code to use "input both in one input line" to suit your needs. – Patrick Artner Jun 23 '18 at 08:04