gpt4 book ai didi

python - 我有我的伪代码,但我无法在 python 中编写循环之一

转载 作者:行者123 更新时间:2023-12-01 01:48:17 30 4
gpt4 key购买 nike

嗨,我对 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”。

最佳答案

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

while numberOfItem != 2:
...

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

#initialise variable
nuberOfItem=0

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

关于python - 我有我的伪代码,但我无法在 python 中编写循环之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998798/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com