在文本冒险游戏中,“TypeError:'int'对象不可调用”

时间:2017-09-27 22:17:31

标签: python typeerror

我正在为我的历史课写一篇文字冒险游戏。我还是Python的新手,所以请耐心等待。

# Massachusetts Text Adventure
#
#      Written by Nick Decker
#
#            In Python 3.6.2

import time
from random import *

startgame = False
food=100
drink=100
morale=80
people=64
health=80
week=1
start=5

def week():
    global week
    global food
    global drink
    global morale
    global people
    global health
    global start
    print ("\n" * 100)
    print("Week number",week)
    print("Food left: ",food)
    print("Drink left: ",drink)
    print("Morale: ",morale)
    print("People alive: ",people)
    ftp = input("How much food will you feed your people? Choose a number between 0-10.")
    if ftp==0:
        print("You did not feed your people.")
        health = health - 5
        morale = morale - 10
    elif ftp <= 5 and ftp != 0:
        print("You gave your people some food.")
        health = health - 2
        food = food - ftp
        morale = morale - 2
    elif ftp <= 10 and ftp > 5:
        print("You gave your people a lot of food.")
        health = health + 2
        food = food - ftp
        morale = morale + 2
    dtp = input("How much water will you give your people? Choose a number between 0-10.")
    if dtp==0:
        print("You did not give your people any water")
        health = health - 10
        morale = morale - 10
    elif dtp <=5 and dtp != 0:
        print ("You gave your people some water.")
        health = health + 1
        morale = morale + 5
        drink = drink - dtp
    elif dtp > 5 and dtp <= 10:
        print("You gave your people a lot of water")
        health = health + 5
        morale = morale + 5
        drink = drink - dtp

    if health <= 70:
        dead = randint(1,7)
        people = people - dead

    if morale <= 65:
        com = randint(1,100)
        if com <= 50:
            morale = morale
        elif com > 50 and com <= 100:
            mutiny()
    week = week + 1
    whattodo()

def whattodo():
    if week > 8:
        print("You've made it to the New World!")
        win = input("To play again, type RESTART. To quit, type QUIT.")
        if win==RESTART:
            story()
        elif win==QUIT:
            quit()
    else:
        week()

def background():
    global start
    print ("\n" * 100)
    print("Congratulations! You have been appointed by the King himself to lead a journey to Massachusetts!")
    time.sleep(2.0)
    print("You are leading a fleet of 11 ships, though in this game, you will be in charge of only one. This ship's name is the Arbella.")
    time.sleep(2.0)
    print("The voyage will take months, and you have to keep the people alive and happy.")
    time.sleep(2.0)
    print("By controlling food and drink you can keep your people alive.")
    time.sleep(2.0)
    print("Good luck!")
    time.sleep(5.0)
    start=0
    week()

def story():
    global startgame
    global food
    global drink
    global morale
    global health
    global people
    global week
    global start

    startgame = True
    food=100
    drink=100
    morale=80
    people=64
    health=80
    week=1

    print ("\n" * 100)
    print('Welcome to the Massachusetts Text Adventure!')
    time.sleep(2.0)
    print('In this game you are on your way to the New World as John Winthrop.')
    time.sleep(2.0)
    print('Along the way, you will face many dangers.')
    time.sleep(2.0)
    print('You will make friends...')
    time.sleep(2.0)
    print('... and enemies.')
    time.sleep(5.0)
    background()

def mutiny():
    print ("\n" * 100)
    global morale
    print("The people on your ship are unhappy! They are planning a mutiny!")
    time.sleep(2.0)
    print("The mutiny has a 50/50 chance of being succesfull.")
    ms = randint(1,100)
    if ms <= 50:
        print("The mutiny failed, and the crew's morale has been boosted by 15!")
        morale = morale + 15
        whattodo()
    elif ms > 50 and ms <= 100:
        fail = input("You've failed! Type RESTART to restart. Type QUIT to quit.")
        if fail==RESTART:
            story()
        elif fail==QUIT:
            quit()

while startgame==False:
    story()

当我运行程序时,它会继续,直到在后台()中调用week()。然后它给了我这些错误:

Traceback (most recent call last):
  File "E:\MassTA.py", line 154, in <module>
    story()
  File "E:\MassTA.py", line 133, in story
    background()
  File "E:\MassTA.py", line 102, in background
    week()
TypeError: 'int' object is not callable

同样,我还是很新的,所以如果这是一团糟,我很抱歉。 有人可以告诉我如何解决这些错误吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

在程序开始时,您将变量周指定为int:week = 1.这与您的函数混淆:week()。重命名整数或函数以更正您的问题。