How to recalculate inputted values for a new inputted value in Python?

47 views Asked by At
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

The error is:

 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'
>>> 

How do I make it so my code recalculates the previous item quantities and units for the new number of people?

Thanks

1

There are 1 answers

6
Vishnu Upadhyay On

You recipe variable is a container which not carry all integer value, so when you use typecasting over your sequence then casting raises literal error take a look:-

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