我是一名初学Python程序员,我正在创建一个卡路里计数器作为实验室。我要做的是在列表中取卡路里的整数值,然后乘以列表中数量的整数值。
代码可以在下面找到
我想要做的基本上是
calories = int((conv(carbs, "c") + conv(fats, "f") + conv(protein, "p") * qtyList[c]))
但是它说c
没有定义(这是因为我没有在displayList函数中这样做,但我不知道如何在本地将c合并到addItem函数中,或者怎么做一开始就是这样。)
有关该计划的更多信息和问题,以及它需要做什么:
(a - add item, d - delete item, l - display list, q - quit)
当它是a
时,接收字符串项目名称,int项目数量,转换为卡路里的int值,将卡路里作为int返回到全局变量,然后乘以数量*卡路里总项目卡路里
d
- 通过列表中的位置从列表中删除项目
l
- 显示列表..简单
q
- 退出
我很想知道是否有人可以帮我解决这个问题,但不是整件事。感谢。
# New Lab 7
# define lists
itemList = []
qtyList = []
calsList = []
totalCal = 0
def conv(grams, kind):
if kind == "f":
return grams * 9
else:
return grams * 4
def dispMenu():
print ("a - add item")
print ("d - delete the item")
print ("l - display the list so far")
print ("q - quit")
def addItem():
carbs = 0
fats = 0
protein = 0
calories = 0
item = input("Item name: ")
if item.isspace() or len(item) == 0:
print("item must have a proper name")
return None # get me outta here
try:
qty = int(input("quantity: "))
if qty <= 0:
print("quantity must be a positive number")
return None
except:
print("That was not a valid integer.")
return None
carbs = int(input("How many grams of carbs are displayed on your item? "))
fats = int(input("How many grams of fats are displayed on your item? "))
protein = int(input("How many grams of protein are displayed on your item? "))
global calories
calories = int((conv(carbs, "c") + conv(fats, "f") + conv(protein, "p") * qtyList[c]))
calsList.append(calories)
itemList.append(item)
qtyList.append(qty)
print("Your item contains", calories, "calories.")
global totalCal
totalCal += calories
return totalCal
def dispList():
if len(itemList) == 0:
print("\nThere are no items in your list\n")
else:
print("\n\n\nThe items in your list are")
print("Itm\tItem\t\tQty\tCals")
totalQty = int(0)
for c in range(len(itemList)):
print(str(c+1)+".\t" + itemList[c], "\t", qtyList[c])
totalQty += qtyList[c]
print("Total calories:\t{}".format(totalCal) + ".\n\n\n")
def delItem():
if len(itemList) == 0:
print("\nThere are no items in your list to delete\n")
else:
print("Please choose the item number to delete")
dispList()
choice = int(input("Delete item > "))
if 1 <= choice <= len(itemList):
del itemList[choice - 1]
del qtyList[choice - 1]
print("Item Deleted")
dispList()
else:
print("\nThe value is out of range\n")
# start the program
print("Welcome to Clinton's Calorie Counter!")
dispMenu()
while True:
choice = input("> ")
if choice == "a":
addItem()
dispMenu()
continue
elif choice == "q":
dispList()
print("Goodbye!")
break
elif choice == "l":
dispList()
dispMenu()
continue
elif choice == "d":
delItem()
continue
答案 0 :(得分:1)
使用(defun make-bar-from-foo (existing-foo c)
(make-instance 'bar :a (a existing-foo) :b (b existing-foo) :c c))
:
qty
您稍后将使用calories = int(conv(carbs, "c") + conv(fats, "f") + conv(protein, "p") * qty)
填写您的列表:qty
使用当前值应该有效。
答案 1 :(得分:0)
我怀疑你的意思是:
$("#btnAdd").click(function () {
$("#example-advanced-form h3>fieldset").height(500);
});
更重要的是,你的意思是:
calories = int((conv(carbs, "c") + conv(fats, "f") + conv(protein, "p") * qty))
其他方面,计算出的数字为:calories_per_item = int((conv(carbs, "c") + conv(fats, "f") + conv(protein, "p"))
calories = calories_per_item * qty
即,使用您刚从用户请求的数量。稍后在代码中,您会将其附加到carbs + fats + (protein * quantity)
。