Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我的字典不能正常使用_Python 3.x_Dictionary - Fatal编程技术网

Python 3.x 我的字典不能正常使用

Python 3.x 我的字典不能正常使用,python-3.x,dictionary,Python 3.x,Dictionary,我在做一个tkinter项目。这只是一个简单的餐厅收银员,不管你怎么称呼它。它还没有完成,但我遇到了一个问题 这是我的密码: from tkinter import * from tkinter.ttk import Style # The different type of items that the store is selling drinks = {} burgers = {} french_fries = {} ice_creams = {} class Object:

我在做一个tkinter项目。这只是一个简单的餐厅收银员,不管你怎么称呼它。它还没有完成,但我遇到了一个问题

这是我的密码:

from tkinter import *
from tkinter.ttk import Style

# The different type of items that the store is selling
drinks = {}
burgers = {}
french_fries = {}
ice_creams = {}

class Object:

    def __init__(self, name, price, type):
        self.name = name
        self.price = price
        self.type = type
        exec('{}["{}"] = [{}, {}]'.format(self.type, name, price, self.type))

    def return_price(self):
        return self.price

    def get_price(self, amount):
        return self.price * amount

# Creating the object
Object('Coco Cola', 1.50, 'drinks')
Object('Sprite', 1.50, 'drinks')
Object('Apple Juice', 2.50, 'drinks')
Object('Orange juice', 2.50, 'drinks')
Object('Grape Juice', 2.50, 'drinks')
Object('Cheese and Ham', 3.75, 'burgers')
Object('Cheese', 4.00, 'burgers')
Object('Cheese and Fish', 3.50, 'burgers')
Object('All Meat', 5.50, 'burgers')
Object('Waffle Fries', 2.50, 'french_fries')
Object('Chili Cheese Fries', 2.75, 'french_fries')
Object('Polenta Fries', 3.50, 'french_fries')
Object('Potato Wedges', 3.50, 'french_fries')
Object('Strawberry', 3.50, 'ice_creams')
Object('Blueberry', 3.75, 'ice_creams')
Object('Black Berry', 3.00, 'ice_creams')
Object('Vanilla', 3.00, 'ice_creams')
Object('Chocolate', 3.50, 'ice_creams')



# This code is what gives me the result i specified at the bottom of my question
print(drinks)

# All the tkinter stuff
root = Tk()
root.resizable(width=False, height=False)
root.style = Style()
root.style.theme_use('xpnative')


drink, burger, french_fry, ice_cream = StringVar(), StringVar(), StringVar(), StringVar()
drink_amount, burger_amount, french_fry_amount, ice_cream_amount = IntVar(), IntVar(), IntVar(), IntVar()
subtotal, tax, total, change = IntVar(), IntVar(), IntVar(), IntVar()
drink_amount.set(0)
burger_amount.set(0)
french_fry_amount.set(0)
ice_cream_amount.set(0)

Label(root, text='Restaraunt Cashier').grid(row=0, column=1, columnspan=5)
Label(root, text='Drinks:').grid(row=1, sticky=W)
Label(root, text='Burgers:').grid(row=2, sticky=W)
Label(root, text='French Fries:').grid(row=3, sticky=W)
Label(root, text='Ice cream:').grid(row=4, sticky=W)
Label(root, text='Amount:').grid(row=1, column=2, sticky=E)
Label(root, text='Amount:').grid(row=2, column=2, sticky=E)
Label(root, text='Amount:').grid(row=3, column=2, sticky=E)
Label(root, text='Amount:').grid(row=4, column=2, sticky=E)
Label(root, text='Subtotal:').grid(row=1, column=4, sticky=W)
Label(root, text='Tax:').grid(row=2, column=4, sticky=W)
Label(root, text='Total:').grid(row=3, column=4, sticky=W)
Label(root, text='Change:').grid(row=4, column=4, sticky=W)

# Entry types
drink_entry = OptionMenu(root, drink, *drinks)
burger_entry = OptionMenu(root, burger, *drinks)
french_fries_entry = OptionMenu(root, french_fry, *drinks)
ice_cream_entry = OptionMenu(root, ice_cream, *drinks)

# Entry amount
drink_entry_amount = Entry(root, width=50)
burger_entry_amount = Entry(root, width=50)
french_fry_entry_amount = Entry(root, width=50)
ice_cream_entry_amount = Entry(root, width=50)

# Subtotal, tax, total, change, entries
subtotal_entry = Entry(root, width=50)
tax_entry = Entry(root, width=50)
total_entry = Entry(root, width=50)
change_entry = Entry(root, width=50)

# Gridding
drink_entry.grid(row=1, column=1, ipady=5)
burger_entry.grid(row=2, column=1, ipady=5)
french_fries_entry.grid(row=3, column=1, ipady=5)
ice_cream_entry.grid(row=4, column=1, ipady=5)
drink_entry_amount.grid(row=1, column=3, ipady=5)
burger_entry_amount.grid(row=2, column=3, ipady=5)
french_fry_entry_amount.grid(row=3, column=3, ipady=5)
ice_cream_entry_amount.grid(row=4, column=3, ipady=5)
subtotal_entry.grid(row=1, column=5, ipady=5)
tax_entry.grid(row=2, column=5, ipady=5)
total_entry.grid(row=3, column=5, ipady=5)
change_entry.grid(row=4, column=5, ipady=5)

entry_list = [
    'drink_entry', 'burger_entry', 'french_fries_entry', 'ice_cream_entry', 'drink_entry_amount', 'burger_entry_amount',
    'french_fries_entry_amount', 'ice_cream_entry_amount'
]

item_selected = []

def submit_clicked(event=None):
    global item_selected
    drink_amount.set(drink_entry_amount.get())
    burger_amount.set(burger_entry_amount.get())
    french_fry_amount.set(french_fry_entry_amount.get())
    ice_cream_amount.set(ice_cream_entry_amount.get())
    item_selected.append(drink.get())
    item_selected.append(burger.get())
    item_selected.append(french_fry.get())
    item_selected.append(ice_cream.get())
    item_selected = list(filter(None, item_selected))



submit_button = Button(root, text='Submit', command=submit_clicked)
submit_button.grid(row=6, column=1, columnspan=5)
root.grid_rowconfigure(5, minsize=20)


root.mainloop()
我将带您浏览代码:

{'Coca Cola': [1.5, 'drinks'], 'Sprite': [1.5, 'drinks'], 'Apple Juice': [2.5, 'drinks'], 'Orange juice': [2.5, 'drinks'], 'Grape Juice': [2.5, 'drinks']}
声明商店作为字典销售的商品 创建一个类来创建对象,以创建商店中销售的每种类型的商品 在下面的代码中声明获取条目和选项菜单的变量。 制作参赛作品 网格化 制作一个函数,获取条目和选项菜单,并将其设置为上述变量。 我认为这里选择的项目是不必要的

所以,这里的问题是,当我跑步时,它会给我这样的东西:

{'Coco Cola': [1.5, {...}], 'Sprite': [1.5, {...}], 'Apple Juice': [2.5, {...}], 'Orange juice': [2.5, {...}], 'Grape Juice': [2.5, {...}]}
我对代码的期望是:

{'Coca Cola': [1.5, 'drinks'], 'Sprite': [1.5, 'drinks'], 'Apple Juice': [2.5, 'drinks'], 'Orange juice': [2.5, 'drinks'], 'Grape Juice': [2.5, 'drinks']}

我相信我的问题不会干扰tkinter代码,我可能是错的,但我只是希望人们能指出我的错误,因为我现在不能。

正如我认为@Aran Fey在评论中诊断的那样,你的问题与你的exec调用对象有关。\uu init\uuuuuuuuuuuuuuuuuuu,这并没有达到你的预期效果。什么时候做:

exec('{}["{}"] = [{}, {}]'.format(self.type, name, price, self.type))
如果self.type等于某个词典(如饮料)的名称,则运行以下Python语句:

drinks[...] = [..., drinks]
也就是说,您正在将对字典的引用指定为字典中值的一部分。我怀疑您希望该值包含字符串drinks,而不是对名为drinks的字典的引用。为此,您需要格式字符串中的{!r}

但更好的是,完全取消执行呼叫!变量名不应是数据。如果您有一个类似字符串的数据项,并且希望它能够让您查找字典,请将其用作另一个级别字典中的键

因此,不是四个顶级变量:

drinks = {}
burgers = {}
french_fries = {}
ice_creams = {}
如果只制作一个我没有创造性地命名的数据,您可能会想出一个更好的名称:

data = {"drinks": {}, "burgers": {}, "french_fries": {}, "ice_creams": {}}
然后,您可以根据需要为字典编制索引,而不需要exec:

data[self.type][name] = [price, self.type]
代码中还有一些其他问题。非常奇怪的是,您创建对象实例完全是为了它们的副作用,而在创建每个实例之后,您会立即将其丢弃。如果构造函数像TKinter类型经常做的那样将对象插入到数据结构中,这可能是有意义的。如果您只关心副作用,只需将代码从Object.\uuuu init\uuuuuuuu移动到顶级函数中,并删除该类即可


您的几个条目对象似乎引用了错误的词典:burger\u Entry=OptionMenuroot,burger,*饮料可能应该将*burgers作为其最后一个参数,或者如果您按照我上面的建议更改数据结构,则*data['burgers'.

您正在将词典添加到自身中。你还没有告诉我们你想做什么,所以我们无法帮你解决。请发布一个更少的代码,并更多地描述代码应该做什么。如果与问题无关,请删除所有tkinter内容。另外,如果我正确理解了这个问题,如果不使用exec,整个问题可能都会避免。不要使用exec。为什么不使用exec?有什么问题吗?如果没有使用exec,您可能不会犯这个错误。或者,用不同的措辞,exec使代码不必要地难以阅读。你这是什么意思?我明白了。谢谢你帮助我。我真是太感谢你了。但我还有一个问题。使用exec或eval有那么危险吗?在完全控制数据的情况下使用exec或eval并不危险,但正如我在回答中所说,这仍然不是一个好主意。人们经常会说,使用eval或exec是危险的,因为如果不受信任的数据进入正在运行的字符串,调用几乎可以做任何事情。为了尝一尝,试着用一个虚假的价格参数来打电话:对象“可口可乐”,“1.50,饮料];从tkinter导入messagebox作为m;m、 展示坏东西;[0',饮料。exec现在弹出一个窗口,它可以做更多的事情,例如更改密码或删除硬盘。所以,你是说尽量避免使用它们?一般来说,是的,你应该避免使用eval和exec。它们总是很难调试,在某些情况下对安全性非常不利。非常感谢