shoppingList = [('Carrot',2), ('Onion',1), ('Tomato',3)]
amountGiven = 12
for item, price in shoppingList:
openMenu = True
while openMenu:
costumerPick = input('Please choose item from the shoppingList\n')
if costumerPick == 'Carrot':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
elif costumerPick == 'Onion':
print('That would be: ${}'.format(price))
amountGiven = amountGiven -price
elif costumerPick == 'Tomato':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
I want to break out the loop after the customer chooses one of these options
It seems you have the boolean openMenu
and the while loop is only entered if openMenu = True
.
Therefore, one way to solve this is to set openMenu = False
after each of the conditional statements.
You would have:
openMenu = True
while openMenu:
costumerPick = input('Please choose item from the shoppingList\n')
if costumerPick == 'Carrot':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
openMenu = False
elif costumerPick == 'Onion':
print('That would be: ${}'.format(price))
amountGiven = amountGiven -price
openMenu = False
elif costumerPick == 'Tomato':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
openMenu = False
However, it doesn't seem like you really need the while loop since your for loop iterates over all the elements in your shoppingList.
I really didn't understand your question. If you want to break from the while loop, when certain condition is met, use 'break' statement.
shoppingList = [('Carrot',2), ('Onion',1), ('Tomato',3)]
amountGiven = 12
for item, price in shoppingList:
openMenu = True
while openMenu:
costumerPick = input('Please choose item from the shoppingList\n')
if costumerPick == 'Carrot':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
break
elif costumerPick == 'Onion':
print('That would be: ${}'.format(price))
amountGiven = amountGiven -price
break
elif costumerPick == 'Tomato':
print('That would be: ${}'.format(price))
amountGiven = amountGiven - price
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.