Answer to Question #264084 in Python for AlessandroM

Question #264084
We assume that one machine offers four different types of drinks (coffee, coffee with milk, chocolate and chocolate with milk), which cost € 1.50, € 1.80, € 2.10 and € 2.40 respectively. The machine accepts 10, 20 and 50 cent coins, one (1) euro and two (2) euro coins, as well as € 5 and € 10 banknotes, and returns change using only coins. To implement a program, which:
a) To display a selection list (menu) of the offered items (numbered from 1 to 4) with the corresponding price for each, the option 0 to exit the program and then reads the user's choice (preferred item or exit), applying defensive programming to ensure that the user enters a value between 0 and 4.





1
Expert's answer
2021-11-10T16:49:27-0500
drinks_menu = {'1':['Coffee',1.50],
               '2':['Coffee with milk',1.80],
               '3':['Chocolate',2.10],
               '4':['Chocolate with milk',2.40],
               '0':['Exit','']}
print(f'{"Menu list":>23}')
print(f'{"№":>3}{"Beverage type":>20}{"Price":>6}')
for key in drinks_menu.keys():
  print(f'{key:>3}{drinks_menu[key][0]:>20}{drinks_menu[key][1]:>6}')
print('    Enter the correct menu number: ')
while True:
  choise = input('>>> ? ')
  if choise in drinks_menu.keys():
    break
  else:
    print('Incorrect data entered please re-enter')
if choise != '0':
  print(f'Selected drink: {drinks_menu[choise][0]} Price: {drinks_menu[choise][1]}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS