簡體   English   中英

Python 2-TypeError:int()參數必須是字符串,類似字節的對象或數字,而不是“列表”

[英]Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

如何解決此錯誤? 當我嘗試用咸菜加載我的保存時,它給了我

Traceback (most recent call last):
  File "C:\Users\user\Downloads\game.py", line 315, in <module>
    menu()
  File "C:\Users\user\Downloads\game.py", line 261, in menu
    if (0) > int(hunger) or (0) > int(thirst):
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

這就是我加載/保存的方式

with open('objs.pickle', "rb") as f:  
    money = pickle.load(f)
    hunger = pickle.load(f)
    thirst = pickle.load(f)
    energy = pickle.load(f)
    wanted = pickle.load(f)
    gun = pickle.load(f)


with open('objs.pickle', 'ab') as f:  
    pickle.dump([money, hunger, thirst, energy, gun, wanted], f)

首先使用'wb'而不是'ab'僅具有最后一個值

然后您可以使用

with open('objs.pickle', "rb") as f:  
    money = pickle.load(f)
    hunger = pickle.load(f)
    thirst = pickle.load(f)
    energy = pickle.load(f)
    gun = pickle.load(f)
    wanted = pickle.load(f)


with open('objs.pickle', 'wb') as f:  
    pickle.dump(money, f)
    pickle.dump(hunger, f)
    pickle.dump(thirst, f)
    pickle.dump(energy, f)
    pickle.dump(gun, f)
    pickle.dump(wanted, f)

要么

with open('objs.pickle', "rb") as f:  
    money, hunger, thirst, energy, gun, wanted = pickle.load(f)

with open('objs.pickle', 'wb') as f:  
    pickle.dump([money, hunger, thirst, energy, gun, wanted], f)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM