Inventory checker fail

This is the place for queries that don't fit in any of the other categories.

Inventory checker fail

Postby XndrK » Thu Sep 12, 2013 12:04 am

I'm trying to make an inventory program, and this is what I have. Right now, what I'm trying to do is check if the
Code: Select all
request
variable is in
Code: Select all
inventory
.

Code: Select all
"""Simple inventory program
NOTE: This doesn't work yet."""

inventory = {"eggs" : 23,
    "spam" : 30,
    "blue whale" : 2
    }

price = {"eggs" : 1.55,
    "spam" : 1.25,
    "blue whale" : 2
    }
   
money = 5688.08


yin = "y"
request = raw_input("What do you want? We have Spam, eggs, and blue whale. ")
while yin.lower() == "y":
    yin = raw_input("Do you want to buy more? You have %p left. y/n " % (money)
    if request in inventory.values():
        print "That will be %p, please." % (price)
        money -= price
    else:
        print "Try again."
else:
    print "Have a nice day! Please come again."


When I run the code, it returns:

Code: Select all
File "C:\Users\****\My Python Scripts\Inventory.py", line 23
    if request in inventory.values():
                                    ^
SyntaxError: invalid syntax
(added asterisks in the error message to protect privacy)

What's wrong here? How do "in" statements work? Did I mess something else up?

Signed,
Xander

P.S. Is there a way to make the code inline?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Inventory checker fail

Postby micseydel » Thu Sep 12, 2013 12:27 am

You missed a close paren on a previous line. That's the most common cause of weird syntax errors.

What do you mean "make the code inline"?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1369
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Inventory checker fail

Postby Mekire » Thu Sep 12, 2013 1:14 am

XndrK wrote:What's wrong here? How do "in" statements work? Did I mess something else up?

Aside from the syntax error, I do believe you are wanting to check the keys of the dictionary; not the values.

By default the in operator looks at keys when used with dictionaries, so you probably wanted:
Code: Select all
if request in inventory:
or possibly:
Code: Select all
if request in inventory and inventory[request] > 0:

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Inventory checker fail

Postby XndrK » Thu Sep 12, 2013 2:26 am

@Mek,

Yes, I was trying for the keys. I was frustrated and trying to Google the answer, so I changed it to values instead. Oops. :oops:

@micseydel,

By inline, I meant it as putting in little snippets of code (like variables) without inserting a \n.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Inventory checker fail

Postby XndrK » Thu Sep 12, 2013 2:42 am

New problem.

I'm running the program as above, but fixed errors as you mentioned. (I'm too lazy to put in the whole code AGAIN. :P ) Now I'm getting this weirdness.

Code: Select all
What do you want? We have Spam, eggs, and blue whale. augh
Do you want to buy more? You have 5688.08 left. y/n n
Try again.
Have a nice day! Please come again.


Another instance:

Code: Select all
What do you want? We have Spam, eggs, and blue whale. eggs
Do you want to buy more? You have 5688.08 left. y/n y
That will be 1.55, please.
Do you want to buy more? You have 5686.53 left. y/n n
That will be 1.55, please.
Have a nice day! Please come again.


Note that the amount of money I have doesn't change initially.

What do I do?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests