需要帮助在Python上取消勾选为字符串吗
我需要帮助使用Python3.4.3取消勾选,我需要作为一个整数来完成。但是,它会导致一个错误。如何取消勾选以前保存为整数的数据。错误发布在下面需要帮助在Python上取消勾选为字符串吗,python,io,typeerror,pickle,Python,Io,Typeerror,Pickle,我需要帮助使用Python3.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 gl
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()
回溯(最近一次呼叫最后一次):
文件“”,第420行,在run_nodebug中
文件“C:\Users\User\Desktop\concentrating 2.py”,第232行,在
启动()
文件“C:\Users\User\Desktop\concentering 2.py”,第68行,在启动中
出纳()
出纳中第95行的文件“C:\Users\User\Desktop\concentrating 2.py”
销售()
文件“C:\Users\User\Desktop\consuming 2.py”,第113行,出售
如果addsale==1且stock_sandwich>=1:
TypeError:无序类型:_pickle.Unpickler()>=int()
我假设错误表明数据类型不是整数。如何使pickle以整数形式加载文件?不要使用:
pickle.Unpickler(file)
使用:
因此,改变这一行:
stock_sandwich = pickle.Unpickler(file)
进入:
对具有图案的其他线条执行相同操作:
obj = pickle.Unpickler(file)
使用两个不同的文件描述符打开同一个文件进行读写:
f = open('p.pickle', 'wb')
d = open('p.pickle', 'rb')
不要那样做。删除上面显示的这两行。更改pickle的写入和读取功能,如下所示:
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)
在发布完整的回溯后,您遇到了什么错误?我的问题:以下是回溯错误(最近一次调用):文件“”,第420行,在run\u nodebug文件“C:\Users\User\Desktop\Continenting 2.py”第229行,在StartUp()文件“C:\Users\User\Desktop\Continenting 2.py”第65行,在StartUp()文件中“C:\Users\User\Desktop\concentering 2.py”,第92行,在出纳销售()文件“C:\Users\User\Desktop\concentering 2.py”的第110行,如果addsale==1和stock\u sandwich>=1,则在销售中:类型错误:无序类型:UnpicklingError()>=int()>>>进行有问题的更改,您可以发布文件对象的示例吗?我不明白为什么要将同一个文件解压到不同的变量?这是一段非常困难的代码,有所有全局变量,以及每个对象一次的pickle和unpickle。您是否考虑过将4个要被pickle的对象合并到一个列表或类似的列表中,并对它们进行pickle和unpickle一次完成pickle?嘿,我试过了,但它给出了以下错误回溯(上次调用):文件“”,第420行,在run\u nodebug文件“C:\Users\User\Desktop\concentraling 2.py”,第235行,在StartUp()文件“C:\Users\User\Desktop\concentraling 2.py”,第65行,在StartUp LoadPickle()文件中C:\Users\User\Desktop\concentral 2.py”,第34行,在LoadPickle shop\u revenue=pickle.load(d)EOFError:在我的答案中,输入的解决方案已用完。好的。我这样做了,没有更多的错误,但这次它实际上没有对这些变量进行计算(即,随着程序的运行,减少变量的值)好的,没关系。程序现在运行得很好!非常感谢您的帮助。我在程序开始时运行了加载Pickle函数,它运行得很好。
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)