简体   繁体   English

Python 2-如何修复TypeError:必须为整数(类型为str)

[英]Python 2 - How to fix TypeError: an integer is required (got type str)

When i try to get my save it gives me this error i've been searching up other peoples answers but i cant find anything to solve my problem. 当我尝试保存时,它给了我这个错误,我一直在寻找其他人的答案,但是我找不到任何解决方法。

Traceback (most recent call last): 追溯(最近一次通话):

  File "C:\Users\user\Downloads\game.py", line 13, in <module>
    with open('objs.pickle', "latin1", "r") as f:  # Python 3: open(..., 'r')
TypeError: an integer is required (got type str)

How can i fix this? 我怎样才能解决这个问题? This is my whole text game below 这是我下面的整个文字游戏

################################################################################
# python C:\Users\user\Downloads\game.py                                       #   
#                                                                              #
#                                                                              #
#                                                                              #
################################################################################
import datetime
import time
from random import randint
import pickle
user_input = input ("Would you like to continue your save? (1=Yes) (2=No)")
if int(user_input) == 1:
    with open('objs.pickle', "latin1", "r") as f:  # Python 3: open(..., 'r')
        money = pickle.load(f)
else:
    start = 1
    timer2 = 0
    now = datetime.datetime.now()
    timer = ('{0}{1}'.format(now.hour, now.minute))
    hunger = 10
    thirst = 10
    energy = 10
    drinks = 0
    foods = 0
    money = 0
    plan = 150
    drink = 1
    food = 1.25
    wanted = 0
    gun = 0

print ("Chapter 1")
print ("The Big City")
time.sleep(1)
print ("You lost your brother recently and are very sad about it but something about his death bugs you the doctors told you that he died of a heart attack but you dont belive this and think he was murdered he used to write in a diary and he would keep it with him whenever he went to Mcdonalds You decide to move to the Big City")
def Eat():
    try:
        global money, plan, drink, food, drinks, foods, wanted, gun
        print (" ")
        print (" Hello welcome to Mcdonald's how may I help you")
        print ("\r Type 1 for food")
        print ("\r Type 2 for drinks")
        print ("\r Type 3 for money")
        user_input = input ("\r Type 4 to walk over to the shady guy")
        print (" ")
        if int(user_input) == 1:
            print (" ")
            print (" We have Hamburgers, fries, and cancer")
            print ("\r Type 1 for hamburgers")
            print ("\r Type 2 for fries")
            user_input = input ("\r Type 3 cancer")
            print (" ")
            if int(user_input) == 1:
                print (" ")
                print (" That will cost you $1.25")
                print ("\r Type 1 to pay")
                user_input = input ("Type 2 to steal")
                print (" ")
                if int(user_input) == 1:
                    print (" ")
                    print ("Thank you for comming")
                    money = float(money) - float(food)
                    print('You now have {0} Dollars '.format(money))
                    print (" ")
                foods = float(foods) + (1)
                if int(user_input) == 2:
                    print (" ")
                    print ("HEY COME BACK AND PAY!")
                    wanted = float(wanted) + (1) 
                    print('Your wanted level is now {0}'.format(wanted))
                    print (" ")
            elif int(user_input) == 2:
                print (" ")
                print ("Potato!")
                print (" ")
            elif int(user_input) == 3:
                print (" ")
                print ("Sorry we are out of cancer")
                print (" ")
        elif int(user_input) == 2:
            print (" ")
            print ("We have Coke, and Pepsi")
            print ("Type 1 for Coke")
            user_input = input ("Type 2 for Pepsi")
            print (" ")
            if int(user_input) == 1:
                print (" ")
                print ("COKE SUCKS NO DRINKS FOR YOU!!!!!")
                print (" ")
            elif int(user_input) == 2:
                print (" ")
                print ("Nice I LOVE Pepsi Coke sucks that will cost you $1.00")
                print ("Type 1 to pay")
                user_input = input ("Type 2 to steal")
                print (" ")
                if int(user_input) == 1:
                    print (" ")
                    print ("Thank you for comming")
                    print (" ")
                    money = float(money) - float(drink)
                    print (" ")
                    print('You now have {0} Dollars '.format(money))
                    print (" ")
                elif int(user_input) == 2:
                    print (" ")
                    print ("HEY COME BACK AND PAY!")
                    wanted = float(wanted) + (1) 
                    print('Your wanted level is now {0}'.format(wanted))
                    print (" ")
                drinks = float(drinks) + (1)
        elif int(user_input) == 3:
            print (" ")
            print ("So heres the plan you take the register ill destract the others.")
            user_input = input ("Type 1 to do the plan")
            print (" ")
            if int(user_input) == 1:
                plan = (randint(1,20))
                money = float(money) + float(plan)
                print (" ")
                print('You got {0} Dollars for doing the plan'.format(plan))
                print('You now have {0} Dollars '.format(money))
                wanted = float(wanted) + (1) 
                print('Your wanted level is now {0}'.format(wanted))
                print (" ")
        elif int(user_input) == 4:
            print (" ")
            print ("Heres the deal I can sell you a Gun or Hack the wanted list.")
            print ("Type 1 to buy a gun for 500$")
            user_input = input ("Type 2 to hack for 100$")
            print (" ")
            if int(user_input) == 1:
                if money > 500:
                    print (" ")
                    print ("Here take it now run off before the cops come")
                    print (" ")
                    gun = 1
                    money = float(money) - (500)
            if int(user_input) == 2:
                if money > 100:
                    print (" ")
                    print ("Here im done now run off before the cops come")
                    print (" ")
                    wanted = 0
                    money = float(money) - (100)

        print (" ")        
        print ("Type 1 to order")
        user_input = input ("Type 2 for main menu")
        print (" ")
        if int(user_input) == 1 and (money) > 1:
            Eat()
        elif int(user_input) == 2:
            menu()
        else:
            menu()
    except ValueError:
        Eat()


def work():
    global gain
    global money
    global energy
    gain = 1
    from random import randint
    work1 = (randint(1,20))
    hours = (randint(1,8))
    answer = float(work1) * float(hours)
    print (" ")
    print('Bob worked {0} hours and got {1} dollars per hour '.format(hours,work1))
    work2 = input('How much money did bob make')
    print (" ")
    if int(work2) == int(answer):
        print (" ")
        print ("you gained 1 Dollars")
        money = float(money) + float(gain)
        print('You now have {0} Dollars '.format(money))
        print("Type 1 to work again") 
        user_input = input ("Type 2 to go to main menu")
        print (" ")
        energy = float(energy) - float(gain)
        if int(user_input) == 1:
            work()
        if int(user_input) == 2:
            menu()
    else:
        print (" ")
        print ("You lost 1 Dollar")
        if money > 0:
            money = float(money) - float(gain)
            print('You now have {0} Dollars '.format(money))
            print("Type 1 to work again") 
            user_input = input ("Type 2 to go to main menu")
            print (" ")
            energy = float(energy) - float(gain)
            if int(user_input) == 1:
                work()
            if int(user_input) == 2:
                menu()
        else:
            print (" ")
            print('You now have {0} Dollars '.format(money))
            print("Type 1 to work again") 
            user_input = input ("Type 2 to go to main menu")
            print (" ")
            energy = float(energy) - float(gain)
            if int(user_input) == 1:
                work()
            if int(user_input) == 2:
                menu()


def items():
    global energy
    global money
    global hunger
    global thirst
    global foods
    global drinks
    global gun
    print (" ")
    print('You have a wallet (${0})  '.format(money))
    if int(gun) > 0:
        print('You have a gun')
    if int(foods) > 0: 
        print('You have {0} hamburgers type 1 to eat hamburger'.format(foods))
    if int(drinks) > 0:
        print('You have {0} Pepsi type 2 to drink Pepsi'.format(drinks))
    user_input = input('Type 3 to go to main menu')
    if int(user_input) == 1 and int(foods) > 0:
        hunger = float(hunger) + (3)
        energy = float(energy) + (3)
        foods = float(foods) - (1)
    if int(user_input) == 2 and int(drinks) > 0:
        thirst = float(thirst) + (2)
        energy = float(energy) + (1)
        drinks = float(drinks) - (1)
    if int(user_input) == 3:
        menu()
    print (" ")
    menu()
def dead():
    input("gg dude you ded")

def menu():
    global wanted
    global now
    global timer
    global hunger
    global thirst
    global money
    global gun
    now = datetime.datetime.now()
    timer2 = ('{0}{1}'.format(now.hour, now.minute))
    if (0) > (hunger) or (0) > (thirst):
        dead()
    if (wanted) > (9):
        if gun == 1:
            wanted = 9
            print (" ")
            print ("You shot the cops but you are not off their radar")
            print (" ")
        else:
            money = float(thirst) - float(money) * .5
            wanted = 0
            print (" ")
            print ("You got caught by the cops")
            print (" ")
    if (timer2) > (timer):
        thirst = float(thirst) - (1)
        hunger = float(hunger) - (1)
        timer = float(timer) + (1)
        timer = ('{0}{1}'.format(now.hour, now.minute))
        menu()
    print (" ")
    print ("Type 1 to Work")
    print ("Type 2 to go to Mcdonalds")
    print ("Type 3 to see your inventory")
    print('You have {0} / 10 hunger'.format(hunger))
    print('You have {0} / 10 thirst'.format(thirst))
    print('You have {0} / 10 energy'.format(energy))
    print('You have {0} / 10 wanted'.format(wanted))
    user_input = input('You have {0} dollars '.format(money))
    print (" ")
    if int(user_input) == 1:
        work()
    elif int(user_input) == 2 and (money) > 1:
        Eat()
    elif int(user_input) == 3:
        items()
    elif int(user_input) == 795535:
        money = float(money) + (1000)
        menu()
    elif int(user_input) == 4:
        with open('objs.pickle', 'ab') as f:  # Python 3: open(..., 'wb')
            pickle.dump([money], f)

    else:
        print (" ")
        print ("You need more money")
        print (" ")
        menu()
menu()

You saved your file with 您用来保存文件

open('objs.pickle', 'ab') as f

Then, you should open your file like 然后,您应该像这样打开文件

open('objs.pickle', "rb") as f

You have a problem here: 您在这里遇到问题:

with open('objs.pickle', "latin1", "r") as f

Here are the arguments open() takes: 以下是open()接受的参数:

open(file, mode='r', buffering=-1, encoding=None,
         errors=None, newline=None, closefd=True, opener=None)

In your case: 在您的情况下:

open('objs.pickle', "latin1", "r")

file = 'objs.pickle'
mode = "latin1"
buffering = "r"

You should do it like so: 您应该这样做:

open('objs.pickle', 'r', encoding="latin1")

Syntax for Open: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Open的语法:open(文件,模式='r',缓冲= -1,编码=无,错误=无,换行=无,closefd = True,打开=无)

Use this : encoding='UTF-8' 使用这个:encoding ='UTF-8'

open('objs.pickle','r',encoding='latin1') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:类型错误:需要一个整数(得到类型 str) - Python: TypeError: an integer is required (got type str) TypeError:Python中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python Python:TypeError 需要 integer(获取类型 str) - Python: TypeError an integer is required(got type str) 如何修复 mysql python 中的“TypeError: an integer is required (got type IntVar)” - How to fix “TypeError: an integer is required (got type IntVar)” in mysql python TypeError:函数联接时需要一个整数(got type str)-python - TypeError: an integer is required (got type str) on function join - python TypeError:Python Selenium Webdri中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python selenium webdri python / flask:TypeError:需要一个整数(得到类型str) - python/flask: TypeError: an integer is required (got type str) Python Flask-TypeError:必须为整数(类型为str) - Python Flask - TypeError: an integer is required (got type str) 类型错误:在 Python 中打开文件时需要 integer(获取类型 str) - TypeError: an integer is required (got type str) when opening file in Python Pyarrow:TypeError:需要一个整数(得到类型 str) - Pyarrow: TypeError: an integer is required (got type str)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM