Home > OS >  index element creates not needed input
index element creates not needed input

Time:03-15

I want to make a code that would propose a customer one or more of products to add to his shop list if he has total cart less than available budget (for example, he bought products for 120 dollars, and program should ask him if he want to add one or a couple of products (for example for 20 and 30 dollars) in list so budget gets close to 150 dollars limit)

My problem here is that somehow i = 1 creates an unwanted input() process which i cannot explain

I am a newbie in Python so maybe someone can propose the solving of problem or even better version of my code, i'll be greatful for every help!

https://ibb.co/vJwhMw0 link for image here

inp = 'bar'
product_list = ['bar', 'bread', 'milk', 'coconut']
product_price = [10, 24, 30, 25]
indx = product_list.index(inp)
price = product_price[indx]

budget = 150
total = 120
proposal_list = ''
i = 0

while total < budget:
  if (product_price[i]   total) <= budget:
    total  = product_price[i]
    proposal_list  = product_list[i]
    i = i   1

print (f'You can also add: {proposal_list}')

CodePudding user response:

There is some problems with your code. No unwanted input() is created it's just that your while loop is infinite because i never changes because the condition is never true.

while total < budget:
    if i >= len(product_price):
        break
    elif (product_price[i]   total) <= budget:
        total  = product_price[i]
        proposal_list  = product_list[i]
    i  = 1

CodePudding user response:

You code got inside infinite loop right after this line:

while total < budget:

Once the code inside this line is executed

if (product_price[i]   total) <= budget:

total become equal 130. Condition total < budget is still true but condition

if (product_price[i]   total) <= budget:

is not. You need to exit the while loop. You can't do it until you update total.

  • Related