In: Computer Science
Use menu based functionality and validation loops to create a new customer bonus fruit package application for the Dirt to Dish discount produce company.
The program you create will prompt the user to enter a number to select one of three bonus pack options:
1 for Apple pack
2 for Orange pack
3 for Banana pack
**Use input validation to ensure a valid option has been
selected before proceeding.
Use variables to keep track of how many of each fruit a customer will have depending on which bonus pack has been chosen. The amount of fruit per bonus pack is given as follows:
Banana pack: 5 bananas, 2 apples, 2 oranges
Orange pack: 5 oranges, 2 apples, 2 bananas
Apple pack: 5 apples, 2 bananas, 2 oranges
Use menu based functionality and input validation and prompt the user to choose from one of the following recipes:
1. Fruit medley (2 of each fruit)
2. Mixed Apple Pie (3 apples, 1 orange, 1 banana)
3. Banana tower (4 bananas, 2 oranges)
The amount of fruit required for each recipe is shown in
parenthesis.
After a valid recipe choice has been made, subtract the amount of fruit needed for the recipe if the customer has enough fruit. If the customer does not have enough fruit then show a message telling the customer to buy more fruits.
Finally display the amount of fruit remaining from the bonus pack after the recipe selection has been made.
First example run:
This is Dirt to Dish's new customer bonus service! Please select your free fruit package: 1. Apple pack 2. Orange pack 3. Banana pack Please enter 1, 2, or 3: 2 You have chosen: Orange pack You have a total of: 2 apples 5 oranges 2 bananas Which dish would you like to make first? 1. Fruit medley (2 of each fruit) 2. Mixed Apple Pie (3apples, 1 orange, 1 banana 3. Banana tower (4 bananas, 2 oranges Please enter 1, 2, or 3: 2 Mixed Apple Pie chosen Sorry, you need more fruits! The fruit you have left is: 2 apples 5 oranges 2 bananas Thank you!
'''
Python version : 2.7
Python program to create a new customer bonus fruit package application
for the Dirt to Dish discount produce company.
'''
def main():
# variables to store the number of apples, oranges and bananas
numApples = 0
numOranges = 0
numBananas = 0
print("This is Dirt to Dish's new customer bonus service!")
# input of pack choice
print('Please select your free fruit package:')
print('1. Apple pack')
print('2. Orange pack')
print('3. Banana pack')
packChoice = int(raw_input('Please enter 1, 2, or 3:'))
# validate the pack choice and re-prompt in case of invalid values
while(packChoice < 1 or packChoice > 3):
print('Invalid choice.')
packChoice = int(raw_input('Please enter 1, 2, or 3:'))
# set the number of apples, oranges and bananas based on the pack choice
if packChoice == 1:
print('You have chosen: Apple pack')
numApples = 5
numBananas = 2
numOranges = 2
elif packChoice == 2:
print('You have chosen: Orange pack')
numApples = 2
numOranges = 5
numBananas = 2
else:
print('You have chosen: Banana pack')
numApples = 2
numOranges = 2
numBananas = 5
print('\nYou have a total of:')
print('%d apples %d oranges %d bananas' %(numApples,numOranges,numBananas))
# input of dish choice
print('Which dish would you like to make first?')
print('1. Fruit medley (2 of each fruit)')
print('2. Mixed Apple Pie (3apples, 1 orange, 1 banana)')
print('3. Banana tower (4 bananas, 2 oranges)')
dishChoice = int(raw_input('Please enter 1, 2, or 3: '))
# validate the dish choice and re-prompt in case of invalid values
while(dishChoice < 1 or dishChoice > 3):
print('Invalid choice.')
dishChoice = int(raw_input('Please enter 1, 2, or 3:'))
enoughFruits = True
# check if enough fruits are available to create the dish and calculate the number of remaining fruits after creating the dish
if dishChoice == 1:
print('Fruit medley chosen')
enoughFruits = (numApples >= 2 and numBananas >=2 and numOranges >=2)
if enoughFruits :
numApples -= 2
numBananas -= 2
numOranges -= 2
elif dishChoice == 2:
print('Mixed Apple Pie chosen')
enoughFruits = (numApples >=3 and numOranges >=1 and numBananas >=1)
if enoughFruits :
numApples -= 3
numBananas -= 1
numOranges -= 1
else:
print('Banana tower chosen')
enoughFruits = (numBananas >=4 and numOranges >=2)
if enoughFruits :
numBananas -= 4
numOranges -= 2
# if not enough fruits available, then print the message
if not enoughFruits:
print('Sorry, you need more fruits!')
# print the remaining fruits
print('The fruit you have left is:')
# only print those fruits that are remaining, not print 0 valued fruits
if numApples > 0:
print('%d apples' %(numApples))
if numOranges > 0:
print('%d oranges '%(numOranges))
if numBananas > 0:
print('%d bananas' %(numBananas))
print('\nThank you!')
# call the main function
main()
#end of program
Code Screenshot:
Output: