我有我的伪代码,但我不能用python写一个循环

2024-05-04 23:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我对python编码有点陌生,我准备了一个伪代码 但是我不能正确地编写python代码。 这是我的伪代码:

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)

我不知道如何在python代码中循环“until”。你知道吗


Tags: 代码编码inputoutputvaluecountitemsplit
2条回答

“Until”可以通过python中的while不等于循环来实现:

while numberOfItem != 2:
    ...

但是您需要将numberOfItem的不断变化的值合并到循环本身中,以便使它在某个点中断:

#initialise variable
nuberOfItem=0

while numberOfItem != 2:
    Item = input("Please select two items: ").split()
    numberOfItem = len(Item)

while numberOfItem != 2:将循环直到得到2个项目。 如果你最初有两个项目在循环体中,这不会运行循环体-当你在循环体中添加/删除内容并希望在列表中正好有两个项目时,会使用这种检查。你知道吗

您需要以某种方式修改在循环中的条件中检查的值(或者直接在while-lvl中执行while len(yourList) != 2:动态检查),否则您将有一个无休止的循环。你知道吗


您可以使用dict优化代码,以验证是否只给出了有效项。 您可以将输入与金额一起存储到第二个dict中,并在所有输入完成后对其求和,例如:

(代码包含Asking the user for input until they give a valid response方法来验证用户输入)

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

输出:

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

无金额:

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

输出:

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

相关问题 更多 >