Python没有正确加载信息?

问题描述:

我不明白。当我在此代码中运行SaveModule.open函数时,它能够正确保存ShoppingCart.items_in_cart正确的变量,但不能用户。在这段代码中我做错了什么?Python没有正确加载信息?

(我知道这可能不是世界上最高效的代码,它仍然在进展中的工作,只是试图让一切都第一个工作日)。

我在空闲的程序进行了测试,一切正常它应该的方式,我只是不明白我在这里做错了什么。

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

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

我修正了这个问题;它通过在UserModule模块中嵌套用户变量来解决。我不知道为什么修正它;如果任何人都可以回答,请告诉我。