需要帮助在Python上取消作为字符串取消

时间:2016-01-04 09:00:51

标签: python io typeerror pickle

我需要使用Python 3.4.3帮助解决问题,我需要将其作为整数来完成。但是它会导致错误。如何将之前保存的数据作为整数取消。错误发布在下面。

import pickle
import shelve
f = open('p.pickle', 'wb')
d = open('p.pickle', 'rb')

def CreatePickle():
    global shop_revenue
    global stock_coke
    global stock_mint
    global stock_sandwich
    pickle.dump(shop_revenue, f)
    pickle.dump(stock_coke, f)
    pickle.dump(stock_mint, f)
    pickle.dump(stock_sandwich, f)

def LoadPickle(file):
    global shop_revenue
    global stock_coke
    global stock_mint
    global stock_sandwich
    shop_revenue = pickle.Unpickler(file)
    stock_coke = pickle.Unpickler(file)
    stock_mint =  pickle.Unpickler(file)
    stock_sandwich = pickle.Unpickler(file)

shop_revenue = 10000
total = 0.0
sales_cash = 0.0
sales_creditcard = 0.0
sales_octopus = 0.0
cost_sandwich = 5.0
cost_coke = 3.0
cost_mint = 6.0
stock_sandwich = 20
stock_coke = 20
stock_mint = 20
password_manager = ""
password = ""
accounts = ["0","0","0","0","0","0","0"]
names = ["0","0","0","0","0","0","0"]
def StartUp():
    global names
    global accounts
    global password
    global password_manager
    global shop_revenue
    global stock_coke
    global stock_mint
    global stock_sandwich
    LoadPickle(d)
    userselect = input("1: Cashier \n \n 2: Manager \n \n 3: Quit")
    if userselect == "1":
        password = input("Enter cashier's password")
        if password == "4123":
         print("Hello Michael")
         Cashier()
        elif password == accounts[i]:
            print("Hello", names[i])
            Cashier()
        else:
            print("Invalid Input")
            StartUp()
    elif userselect == "2":
        password_manager = input("Enter manager password")
        if password_manager == "123456":
            Manager()
        else:
            print("Invalid Input")
            StartUp()
    elif userselect == "3":
        CreatePickle()
        quit()
    else:
        print("Invalid input")
        StartUp()
def Cashier():
    global total
    c_optionselect = input("1: Start sale \n \n 2: Quit")
    if c_optionselect == "2":
            StartUp()
    elif c_optionselect == "1":
            print("----Welcome to Star Store----")
            Sale()
    else:
        print("Invalid input")
        Cashier()
def Sale():
    global cost_coke
    global cost_mint
    global cost_sandwich
    global stock_sandwich
    global stock_mint
    global stock_coke
    global sales_cash
    global sales_creditcard
    global sales_octopus
    global total
    global shop_revenue
    addsale = int(input("1: Sandwich \n \n 2: Coke \n \n 3: Mint" \
                         "\n \n 4: Remove Previous \n \n 5:Display Receipt"))
    if addsale == 1 and stock_sandwich >= 1:
        total = total + cost_sandwich
        stock_sandwich = stock_sandwich - 1
        shop_revenue = shop_revenue + cost_sandwich
        print("Sandwich    $",cost_sandwich)
        Sale()
    elif addsale == 2 and stock_coke >= 1:
        total = total + cost_coke
        stock_coke = stock_coke - 1
        shop_revenue = shop_revenue + cost_coke
        print("Coke        $",cost_coke)
        Sale()
    elif addsale == 3 and stock_mint >= 1:
        total = total + cost_mint
        stock_mint = stock_mint - 1
        shop_revenue = shop_revenue + cost_mint
        print("Mint        $",cost_mint)
        Sale()
    elif addsale == 5:
        print("Total is: $", total)
        print("---Thank you for Shopping---")
        method_payment = int(input("Select method of payment \n \n" \
                    "1: Cash \n \n 2: Octopus \n \n 3: Credit Card"))
        if method_payment == 1:
            sales_cash = total + sales_cash
        elif method_payment == 2:
            sales_octopus = total + sales_octopus
        elif method_payment == 3:
            sales_creditcard = total + sales_creditcard
        total = 0
        Cashier()
    else:
        print("Invalid input")
        Sale()

def Manager():
    global sales_cash
    global sales_creditcard
    global sales_octopus
    global cost_coke
    global cost_mint
    global cost_sandwich
    global stock_sandwich
    global stock_mint
    global stock_coke
    global accounts
    global i
    global names
    global shop_revenue
    m_optionselect = input("1: Stock Price \n \n 2: Check inventory" \
                "\n \n 3: Check Sales \n \n 4: Create new account" \
                         "\n \n 5: Buy stock \n \n " \
                         "6: Check Revenue \n \n 7: Quit")
    if m_optionselect == "7":
            StartUp()
    elif m_optionselect == "3":
        print("Sales by cash: $",sales_cash)
        print("Sales by octopus: $",sales_octopus)
        print("Sales by credit card: $",sales_creditcard)
        Manager()
    elif m_optionselect == "2":
            print("Number of sandwiches", stock_sandwich)
            print("Number of cokes", stock_coke)
            print("Number of mint", stock_mint)
            Manager()
    elif m_optionselect == "1":
        stock_change = input("Select item to change price: \n \n" \
                    "1: Sandwich \n \n 2: Coke \n \n 3:Mint")
        if stock_change == "1":
                sandwich_change = float(input("Enter new price"))
                cost_sandwich = 0 + sandwich_change
                Manager()
        elif stock_change == "2":
                coke_change = float(input("Enter new price"))
                cost_coke = 0 + coke_change
                Manager()
        elif stock_change == "3":
                mint_change = float(input("Enter new price"))
                cost_mint = 0 + mint_change
                Manager()
        else:
            print("Invalid input")
            Manager()
    elif m_optionselect == "4":
        number_accounts = int(input("How many new accounts"))
        for i in range(0,number_accounts):
            temp2 = input("Enter names of the account holder")
            temp = input("Enter account password here")
            names[i] = temp2
            accounts[i] = temp
        Manager()
    elif m_optionselect == "5":
        add_stock_choice = input("1: Sandwich \n \n 2: Coke \n \n 3: Mint")
        if add_stock_choice == "1" and shop_revenue >= 4:
            add_stock_sandwich = int(input("$4.00/sandwich \n \n Enter amount"))
            stock_sandwich = add_stock_sandwich + stock_sandwich
            shop_revenue = shop_revenue - 4*(add_stock_sandwich)
            Manager()
        elif add_stock_choice == "2" and shop_revenue >= 2.5:
            add_stock_coke = int(input("$2.50/coke \n \n Enter amount"))
            stock_coke = add_stock_coke + stock_coke
            shop_revenue = shop_revenue - 2.5*(add_stock_coke)
            Manager()
        elif add_stock_choice == "3" and shop_revenue >= 5:
            add_stock_mint = int(input("$5.00/mint \n \n Enter amount"))
            stock_mint = add_stock_mint + stock_mint
            shop_revenue = shop_revenue - 5*(add_stock_mint)
            Manager()
        else:
            print("Invalid Input")
            Manager()
    elif m_optionselect == "6":
        print("The revenue is $",shop_revenue)
        print("Total profit is $",shop_revenue-10000)
        Manager()
    else:
        print("Invalid input")
        Manager()
CreatePickle()
StartUp()

以下是完整错误:

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "C:\Users\User\Desktop\Culminating 2.py", line 232, in <module>
    StartUp()
  File "C:\Users\User\Desktop\Culminating 2.py", line 68, in StartUp
    Cashier()
  File "C:\Users\User\Desktop\Culminating 2.py", line 95, in Cashier
    Sale()
  File "C:\Users\User\Desktop\Culminating 2.py", line 113, in Sale
    if addsale == 1 and stock_sandwich >= 1:
TypeError: unorderable types: _pickle.Unpickler() >= int()

我认为错误表明数据类型不是整数。如何使pickle将文件作为整数加载?

1 个答案:

答案 0 :(得分:0)

不要使用:

pickle.Unpickler(file)

使用:

pickle.load(file)

所以改变这一行:

stock_sandwich = pickle.Unpickler(file)

成:

stock_sandwich = pickle.load(file)

对具有模式的其他行执行相同的操作:

obj = pickle.Unpickler(file)

使用两个不同的文件描述符打开相同的文件进行读写:

f = open('p.pickle', 'wb')
d = open('p.pickle', 'rb')

不要那样做。删除上面显示的这两行。点击你的泡菜写作和阅读功能:

def CreatePickle():
    ...
    with open('p.pickle', 'wb') as fobj:
        pickle.dump(shop_revenue, fobj)
        pickle.dump(stock_coke, fobj)
        pickle.dump(stock_mint, fobj)
        pickle.dump(stock_sandwich, fobj)

def LoadPickle(file):
    ...
    with open('p.pickle', 'rb') as fobj:
        shop_revenue = pickle.load(fobj)
        stock_coke = pickle.load(fobj)
        stock_mint =  pickle.load(fobj)
        stock_sandwich = pickle.load(fobj)