【问题标题】:How to recalculate inputted values for a new inputted value in Python?如何在 Python 中重新计算新输入值的输入值?
【发布时间】:2015-01-29 09:42:36
【问题描述】:
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'
>>> 

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

谢谢

【问题讨论】:

    标签: python indexing recipe


    【解决方案1】:

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

    In [48]: int('12as2')
    ValueError: invalid literal for int() with base 10: '12as2'
    

    【讨论】:

    • 那我该怎么解决?
    • @user3422851 type 实际上你的recipe 是什么?
    • @user3422851 recipe 里面的数据是什么,想想如果它没有实际值或数字,你怎么能把它转换成integer.
    • 那么您如何使用 readlines 扫描一个 txt 文件,然后使用这些数字为不同数量的人重新计算它们?
    • @user3422851 这是数学,你最好在代码中描述错误并应用逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2013-04-19
    • 2021-04-30
    • 2023-03-31
    • 2014-02-22
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多