Python没有正确加载信息?

2024-10-03 02:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我在空闲程序中测试了这个,一切都按照它应该的方式运行,我只是不明白我做错了什么

至于我为什么要这么做,这只是我给自己设定的一个挑战。没别的了

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.')

Tags: inselffalseifdefitemsopenproduct
1条回答
网友
1楼 · 发布于 2024-10-03 02:36:03

我解决了这个问题;通过将users变量嵌套在UserModule模块中来解决此问题。我不知道为什么会这样;如果有人能回答这个问题,请告诉我

相关问题 更多 >