Dev Patel
Dev Patel

Reputation: 35

I have my pseudocode but I can't write one of the loop in python

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.

Upvotes: 1

Views: 162

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51653

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

Upvotes: 0

iacob
iacob

Reputation: 24231

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

Upvotes: 2

Related Questions