我有我的伪代码,但我可以';t用python编写一个循环

我有我的伪代码,但我可以';t用python编写一个循环,python,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, "er

嗨,我对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代码中循环“直到”。

“直到”可以通过
实现,而
不等于python中的循环:

while numberOfItem != 2:
    ...
但是您需要将
numberOfItem
的变化值合并到循环本身中,以便在某个点上使其中断:

#initialise variable
nuberOfItem=0

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

whilenumberofitem!=2:
将循环,直到您得到2个项目。 如果您最初在循环体中有2项,则不会运行循环体-当您在循环体中向列表添加/删除内容并希望在列表中恰好有2项停止时,将使用这种检查

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


您可以使用dict优化代码,以验证只提供了有效项。 您可以将输入与金额一起存储到第二个dict中,并在所有输入完成后将其相加,如下所示:

(代码包含验证用户输入的方法)

输出:

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

无金额:

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

while numberOfItem<2:
-如果循环体中已经有2个项目,则不会运行循环体-当您将项目添加到列表中并希望在2处停止时使用此选项。您需要以某种方式修改您在循环内(或直接在while-lvl)的条件中检查的值,或者您有一个无止境的循环使用while或for循环,并在必要时中断。请阅读“我是否重写该项”。拆分(0和计数在while循环中您的代码运行良好,但如果我想在同一循环中输入内容,该怎么办line@DevPatel
item,amount=输入(“item和amount,空格”).split()
-如果只给出一个项目,这将崩溃,您必须添加
尝试:除ValueError:
之外,还要捕获ppl仅输入一个项目或数字时引入的错误。然后继续执行“dict检查中的项目”和“数字真的是整数吗?”.我不是在寻找金额在这种情况下,我只是在寻找当用户输入两个项目时,我应该在两个产品之间分配总成本written@DevPatel试着去理解代码的作用,你可以消除“数量”的东西(输入+验证)将购物车更改为简单列表,并简化输出和总和计算以满足您的需要。如果您想将两个项目都放在一行中,则必须将它们拆分,并检查它们是否都在
itemCostDic
中。修改最后一个代码以使用“在一行中输入两个项目”来满足您的需要。