嗨,我对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代码中循环“直到”。
答案 0 :(得分:2)
'Until'可以通过scrollable: {
endless: true
},
等于python中的循环来实现:
while
但是您需要将while numberOfItem != 2:
...
的变化值合并到循环本身中,以使其在某个时刻中断:
numberOfItem
答案 1 :(得分:0)
while numberOfItem != 2:
将循环播放,直到您获得2个项目。
如果您最初有2个项目,则不会运行循环主体-当您在循环主体中 中添加/删除列表中的内容并希望在其中的2个项目处停止时,将使用这种检查清单。
您需要以某种方式修改在循环中检查条件的值(或直接在同时进行while len(yourList) != 2:
动态检查-lvl),否则循环将不断。
您使用字典细化代码以验证仅给出有效项。 您可以将输入值与金额一起存储到第二个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