2016-05-12 66 views
0

我不明白。當我在此代碼中運行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.') 

回答

0

我修正了這個問題;它通過在UserModule模塊中嵌套用戶變量來解決。我不知道爲什麼修正它;如果任何人都可以回答,請告訴我。