-4
money = 170
KitKat = 90
choice1 = raw_input("Choose a snack: ")

Choose a snack: KitKat

if choice1 < money:
    print ("Enjoy your snack")
else:
    print ("You cannot afford ") + (choice1)

You cannot afford KitKat

This is a shortened version of a vending machine programme I am working on. You have a certain amount of coins, and it asks you to choose a snack. In this shortened program you always have 170 coins and you can only choose KitKat. It then checks if you can afford KitKat or not by comparing the variables: "money", and "choice1" to see which is bigger. If "money" is bigger, it should say "Enjoy your snack". But if "choice1" is bigger, it should say "You cannot afford KitKat".

The problem is, when I test it, it always thinks that "choice1" is greater than "money" and tells me that I "cannot afford KitKat".

I think maybe when I am inputting "KitKat" it doesn't recognise it as a variable and compares the word "KitKat" to the variable "money", instead of comparing the variable "KitKat" to the variable "money" But I'm not sure. Any ideas where I'm going wrong?

3 Answers3

5

why not have a dictionary with the prices of various snacks.

i.e instead of

KitKat=90

have

price ={}

price["KitKat"]=90

then you can change the line

if choice1 < money:

to

if price[choice1] < money:
user3684792
  • 2,542
  • 2
  • 18
  • 23
  • I'm a noob to Python so I don't fully understand what's going on here. But it works! So thanks buddy. One more thing, after that bit I have a line: money = money - choice1. The program does a syntax error when I get to that bit. – user3937516 Aug 14 '14 at 15:54
  • @user3937516 remember that `choice1` is a string, and `money` is an int. – Al.Sal Aug 14 '14 at 16:06
  • so a dictionary is an object that allows you to look up the value of certain keys. i.e the line price[choice1] < money looks up the value associated with choice1 ( in this case 90) and compares it to money. Before what you were doing was comparing "KitKat" to 90, so it didn't work. Can you see how to use this to fix your line? – user3684792 Aug 15 '14 at 08:33
  • I think I understand now. Thanks again user 3684792, and Al.Sal. I should be able to use that dictionary thing to fix the line "money = money - choice1" as well. Maybe "money = money - price[choice1]" would work. Idk, I'll play around with it. – user3937516 Aug 15 '14 at 10:06
0

I'd recommend going with @user3684792's answer but if you wanted to do it this way you could use

choice1 = globals()[raw_input("Choose a snack: ")]

but I wouldn't advise it. I imagine it'd cause problems if the user enters unexpected things.

Holloway
  • 6,412
  • 1
  • 26
  • 33
0

Your choice could be KitKat, but that's still just a string 'KitKat' and isn't linked in anyway to the variable KitKat.

If you're avoiding using dictionaries (the best way to do it) you can use if functions.

It's messy but would work.

#define all prices
price_kitkat = 90
price_lolly = 12
price_twirl = 100

#define amount of money
money = 170

#Make your choice
choice1 = raw_input("Choose a snack: ")

#Find price of choice
if choice1 == 'KitKat':
    price = price_kitkat
elif choice1 == 'Lolly':
    price = price_lolly
elif choice1 == 'Twirl':
    price = price_twirl
elif choice1 == '':
    print "You didn't choose a snack!"
else:
    print "Your snack doesn't exist!"

#can you afford it?
if price <= money:
    print "You purchased a snack!
else:
    print "You can't afford that!"
Joe Smart
  • 751
  • 3
  • 10
  • 28
  • Yeah that is really messy. I'll go with the other guy's answer. Thanks though! – user3937516 Aug 14 '14 at 15:55
  • I know his answer was better, the reason I still posted this was in case you had a burning desire to use variables (or if your homework specified you had to!) Learning to use `if` functions is really useful, so understanding how this way works would be pretty good for learning even though I agree with using the other answer) – Joe Smart Aug 14 '14 at 15:57
  • Yeah, I understand if functions, it's ok. And the homework doesn't specify anything. – user3937516 Aug 14 '14 at 16:00