Python没有正确加载信息?

时间:2016-05-12 17:22:49

标签: python

我不明白。当我在此代码中运行 SaveModule.open 函数时,它能够正确地将 ShoppingCart.items_in_cart 保存在正确的变量中,但不能用户。我在这段代码中做错了吗?

(我意识到这可能不是世界上最有效的代码,它仍然是一项正在进行中的工作。只是试图让一切工作先行。)

我在IDLE程序中对此进行了测试,一切都按照预期的方式进行,我只是不知道我在这里做错了什么。

至于为什么我要这样做,这只是我为自己设定的挑战。没什么。

import os
import linecache
import ast
users = {}
logged_in = False
mspw = '00415564'

class ShoppingCart(object):
    #Creates shopping cart objects for users of our fine website.
    items_in_cart = {}
    def __init__(self, customer_name):
        self.customer_name = customer_name
    def __repr__(self):
        return(self.customer_name)
    def __str__(self):
        print('Hello ' + self.customer_name)
        print('You have ' + str(len(self.items_in_cart)) + ' items in your cart.')
        for item in self.items_in_cart:
            print('     ' + item, self.items_in_cart[item])
        return('Thank you for shopping at Job Simulator!')
    def add_item(self, product, price):
        #add a product to the cart
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price
            print(product + ' added.')
        else:
            print(product + ' is already in the cart.')
    def change_price(self, product, price):
        if product in self.items_in_cart:
            self.items_in_cart[product] = price
            print('Price Changed')
        else:
            print('Impossible!')
    def change_item(self, product, newproduct):
        if product in self.items_in_cart:
            tempstor = self.items_in_cart[product]
            del self.items_in_cart[product]
            self.items_in_cart[newproduct] = tempstor
            print('Item changed')
        else:
            print('Impossible')
    def remove_item(self, product):
        #Remove product from the cart.
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print(product + ' removed.')
        else:
            print(product + ' is not in the cart.')


class UserModule(object):
    def add_user(usnm, pswd):
        if logged_in == False:
            if not usnm in users:
                users[usnm] = pswd
                s = True
            else:
                s = False
        else:
            s = False
        return s
    def remove_user(usnm, pswd):
        if logged_in == False:
            if usnm in users:
                if pswd == users[usnm]:
                    del users[usnm]
                    s = True
                else:
                    s = False
            else:
                s = False
        else:
            s = False
        return s
    def check_users(mst):
        if mst == mspw:
            for item in users:
                print('     ' + item, users[item])
        else:
            print('Need master password')

class SaveModule(object):
    def save(file):
        svflu = open(file + '.sbcu', 'w')
        svfli = open(file + '.sbci', 'w')
        svflu.truncate()
        svfli.truncate()
        svflu.write(str(users))
        svfli.write(str(ShoppingCart.items_in_cart))
        svflu.close()
        svfli.close()
    def open(file):
        if os.path.isfile(file + '.sbcu') and os.path.isfile(file + '.sbci'):
            svfl = open(file + '.sbcu', 'r')
            users = ast.literal_eval(linecache.getline(file + '.sbcu', 1))
            svfl.close()
            svfl = open(file + '.sbci', 'r')
            ShoppingCart.items_in_cart = ast.literal_eval(linecache.getline(file + '.sbci', 1))
            svfl.close()
        else:
            print('This file doesn\'t exits.')

1 个答案:

答案 0 :(得分:0)

我解决了这个问题;它是通过在 UserModule 模块中嵌套用户变量来解决的。我不知道为什么修复它;如果有人能回答,请告诉我。