0
print("For this recipe ", recipe[0], "for ", recipe[1])
print("You will need the following Ingredients with quantities and units: ")
Index = 2
while (Index != len(recipe)) :
    print(recipe[Index])
    Index = Index+1
    calculatedIQuantity = int(recipe[Index]) / int(recipe[1]) * int(peopleNo)
    print(calculatedIQuantity)
    Index = Index+1
    print(recipe[Index])
    Index = Index+1

错误是:

 Traceback (most recent call last):
  File "\\server\examuser41\test1.py", line 78, in <module>
    viewer()
  File "\\server\examuser41\test1.py", line 70, in viewer
    calculatedIQuantity = int(recipe[Index]) / int(recipe[1]) * int(peopleNo)
ValueError: invalid literal for int() with base 10: 'cm\n'
>>> 

如何使我的代码为新的人数重新计算以前的项目数量和单位?

谢谢

4

1 回答 1

0

recipe的变量是一个不携带所有整数值的容器,所以当你使用typecasting你的序列时,casting请看literal error 一下:-

In [48]: int('12as2')
ValueError: invalid literal for int() with base 10: '12as2'
于 2014-12-01T11:36:50.550 回答