On 4/10/2014 6:26 PM, Saba Usmani wrote:

 My task is :
Welcome to the tutor list. In what school are you learning Python?

What version of Python? What operating system? What do you use to write and run your code?

What Python elements have you studied so far? Your code can be greatly simplified by the application of tuples, lists, dictionaries, functions and/or classes.

Requests: post in plain text rather than html (no formatting). This guarantees that all of us will be able to read your posts with ease.

Also reply in such a way that a copy goes to tutor@python.org, to keep all of us in the loop.

The rest of my comments follow the relevant part of your post. Please also reply in similar fashion. Avoid as much as possible "top posting".

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 am glad to see this problem appear again. Recently it was sent to me privately by someone who claimed it was not homework!

Critique of the specification: it is a bit vague (imprecise). I assume the instructor has a broad tolerance for what is delivered. In a business environment I'd want it a lot more precise. Often I have to help the user accomplish this, as many users don't know how to do this.

it is a good idea to first develop a sample dialog from the specification, review that with the user, then code to reproduce that sample. Do this. Either review it with the instructor or forget that step.

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?
Efficient? Do you mean execution time? With today's processor speeds that is rarely an issue to be concerned about when first writing code.

Complexity does not necessarily create efficiency.
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?
Alan has given a suggestion already.

Based on your code you already know how to use a while loop. What is mysterious about using it here?


Code:

print "Welcome to Snack Attack"

snack1 = 0.40
snack2 = 0.75
snack3 = 1.20
snack4 = 0.99
snack5 = 0.50
insert = 0
You never use this variable!

change = 0
This machine does not dispense change!
currentCredit = 0.00
A = 0.10
B = 0.20
C = 0.50
D = 1.00
a = 0.10
b = 0.20
c = 0.50
d = 1.00
Never Never use floating values for money, as floating point cannot in general represent fractional values exactly.

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?

You don't. Better (as Alan suggested) use raw_input and treat users entries as character. There is no advantage to using input and integers and a lot of room for errors.

choice = input("Select your snack: ")
This does not agree with the specification - enter coin(s) first. The ensuing dialog also does not agree with the specification. You deposit one or more coins first, see the available credit. then choose a

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
Major problem here. As A user I'd enter (say) 20p. How does that get translated to a numeric value for adding?
What should happen if I enter 30p, or 20x, or foo?
      print "Your current credit is £",currentCredit
This assumes that credit less than £1 will be reported as a fraction of a £. How will you handle this fraction?
  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"

Otherwise it is an OK program, for a beginner.

You should carefully develop a sample dialog, by taking the specification item by item, translating each piece into a bit of dialog, then write code from that. It will be quite different.

If you have studied lists or tuples, consider putting all the data for one product in a list, and create another list of these lists. By doing this you enter the data once, and refer to it more than once.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to