# [Tutor] Refining Code

Saba Usmani sabausmani at outlook.com
Fri Apr 11 00:26:09 CEST 2014

```My task is :A food vending machine accepts 10p, 20p, 50p and £1 coins. One or more coins are inserted and the current credit is calculated and displayed. A product is selected from those available. The system checks to see if there is enough credit to purchase the product chosen. If there is not enough credit the system displays an error message. If there is enough credit it dispenses the product, updates the credit available and displays the remaining credit. Further selections can be made if there is enough credit. The vending machine simulation should have five products and prices. Design, code, test and evaluate a program for this simulation.
I have designed the following code, but would like to know how to make it more efficient without making it too complex as I am a beginner or is this fine? Also, how do I add a loop to this so that once one product has been dispensed the program asks the user if they would like to continue and purchase another product?

Code:
print "Welcome to Snack Attack"
snack1 = 0.40snack2 = 0.75snack3 = 1.20snack4 = 0.99snack5 = 0.50insert = 0
change = 0currentCredit = 0.00A = 0.10B = 0.20C = 0.50D = 1.00a = 0.10b = 0.20c = 0.50d = 1.00
print "Menu"print "Snack 1: Snickers - £0.40"print "Snack 2: Doritos - £0.75 "print "Snack 3: J20 - £1.20"print "Snack 4: Oreos - £0.99"print "Snack 5: M&M's - £0.50" print "Exit?"                - how do I make this a Boolean expression, so the user can respond with either yes or no?
choice = input("Select your snack: ")
if choice==1:   print " "  print "You have selected Snickers, which cost £0.40"  print "Please insert £0.40"  while currentCredit < snack1:      print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:£1"      insert_coins = input("Insert coins: ")      currentCredit = insert_coins + currentCredit      print "Your current credit is £",currentCredit  else:      change_given=currentCredit-snack1      print " "      print "Your change is £",change_given      print "Your Snickers have been dispensed...Enjoy!"
elif choice==2:     print "You have selected Doritos, which cost £0.75"     print "Please insert £0.75"     while currentCredit<snack2:      print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:£1"      insert_coins = input("Enter coins: ")      currentCredit = insert_coins + currentCredit      print "Your current credit is £",currentCredit     else:      change_given=currentCredit-snack2      print " "      print "Your change is £",change_given      print "Your Doritos have been dispensed...Enjoy!"
elif choice==3:     print "You have selected J20, which costs £1.20"     print "Please insert £1.20"     while currentCredit<snack3:      print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:£1"      insert_coins = input("Enter coins: ")      currentCredit = insert_coins + currentCredit      print "Your current credit is £",currentCredit     else:      change_given=currentCredit-snack3      print " "      print "Your change is £",change_given      print "Your J2O has been dispensed...Enjoy!"
elif choice==4:     print "You have selcetd Oreos, which cost £0.99"     print "Please insert £0.99"     while currentCredit<snack4:      print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:£1"      insert_coins = input("Enter coins: ")      currentCredit = insert_coins + currentCredit      print "Your current credit is £",currentCredit     else:      change_given=currentCredit-snack4      print " "      print "Your change is £",change_given      print "Your Oreos have been dispensed...Enjoy!"
elif choice==5:     print "You have selected M&M's, which cost £0.50"     print "Please insert £0.50"     while currentCredit<snack5:      print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:£1"      insert_coins = input("Enter coins: ")      currentCredit = insert_coins + currentCredit      print "Your current credit is £",currentCredit     else:      change_given=currentCredit-snack5      print " "      print "Your change is £",change_given      print "Your M&M's have been dispensed...Enjoy!"
elif choice=="Exit":    print "Thank You and Have a Nice Day"   else:    print " "    print "Invalid choice"
Thanks,Saba

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140410/25ae5b0f/attachment.html>
```