我有以下代码主要工作。我只需要它允许用户输入“插件”,直到他们输入sentinel值退出循环,并让它添加选择。这是一个课堂作业,但这是所有我有困难。任何帮助都将不胜感激。你知道吗
#start
def main(): #create a module - python function to hold main processing
#declarations
addinArray = ["Whipped Cream", "Cinnamon", "Chocolate sauce", "Amaretto", "Irish whiskey"]
priceArray = [0.89, 0.25, 0.59, 1.50, 1.75] #this array holds the add-in prices
loopFlag = "" #sentinal value for looping
cnt = 0
COFFEECOST = 2 #constant to coffee cost
addinName = ""
totalCost = 0
#input enter add-in name
addinName = input("Enter on Keyboard Coffee Add-in (ex. Whipped Cream, Cinnamon, etc.) or Done to quit")
while (addinName != "Done"): #loop until addinName is Done
while (cnt < len(addinArray)):
if (addinName == addinArray[cnt]) :
totalCost = priceArray[cnt] + totalCost
print("Add-in ", addinName, " with a price of ", priceArray[cnt])
print( "with a total add-in price so far of ", totalCost)
cnt = cnt + 1 #increment counter for next element in array
#end while
addinName = input("Enter on Keyboard Coffee Add-in (ex. Whipped Cream, Cinnamon, etc.) or Done to quit")
#end while
totalCost = totalCost + COFFEECOST
print("Total cost is: ", totalCost)
main()
#stop
我认为
cnt = 0
应该在第一个循环之后重置:如果没有这一点,在第一个外接程序之后,cnt就可以到达
len(addinArray)
,并且在为新的外接程序执行循环时永远不会出现第二个外接程序。你知道吗我在本地运行了你的代码,发现了两件事。你知道吗
while
循环之后将cnt
变量重置为零,否则它将不会再次执行,因为条件cnd>5
将始终是true
。你知道吗input()
调用似乎抛出了一个错误,因此我测试了raw_input()
,似乎正常工作。你知道吗希望这有帮助!
相关问题 更多 >
编程相关推荐