Python不导入数据结构

时间:2018-03-30 18:49:21

标签: python python-3.x

我正在尝试创建一个基于文本的游戏。我做了一个基本/通用的介绍,现在它变得越来越长,所以我想创建一个新文件,以便我可以添加盔甲和武器之类的东西。当我去尝试导入'my_character'时它给了我一个错误,所以我去研究这个问题,我发现了this,所以我尝试了。我刚收到一条错误消息,说它没有定义(类似于下面的错误)。这是名为 intro 的简介部分:

# Date started: 3/13/2018
# Description: text based adventure game

import time


def display_intro():
    print('It is the end of a 100 year war between good and evil that had\n' +
          'killed more than 80% of the total human population. \n')
    time.sleep(3)

    print('The man who will soon be your father was a brave adventurer who \n'
      + 'fought for the good and was made famous for his heroism. \n')
    time.sleep(3)

    print('One day that brave adventurer meet a beautiful woman who he later \n'
    + 'wed and had you. \n')
    time.sleep(3)


def main():
    display_intro()


main()
gen = input('\nYour mother had a [Boy or Girl]: ')
name = input("\nAnd they named you: ")
print("You are a {} named {}".format(gen, name))

chara_class = None

# Assigning points Main
my_character = {
    'name': name,
    'gender': gen,
    'class': chara_class,
    'strength': 0,
    'health': 0,
    'wisdom': 0,
    'dexterity': 0,
    'points': 20
}


# This is a sequence establishes base stats.


def start_stat():
    print("\nThis is the most important part of the intro")
    time.sleep(3)
    print("\nThis decides your future stats and potentially future gameplay.")
    time.sleep(4)
    print("\nYou have 20 points to put in any of the following category: Strength, Health, Wisdom, or Dexterity.\n"
)


def add_character_points():  # This adds player points in the beginnning
    attribute = input("\nWhich attribute do you want to assign it to? ")
    if attribute in my_character.keys():
        amount = int(input("By how much? "))

        if (amount > my_character['points']) or (my_character['points'] <= 0):
            print("Not enough points!!! ")
        else:
            my_character[attribute] += amount
            my_character['points'] -= amount
    else:
        print("That attribute doesn't exist! \nYou might have to type it in all lowercase letters!!!")


def remove_character_points():
    attribute = input("\nWhich of the catagories do you want to remove from? ")
    if attribute in my_character.keys():
        amount = int(input("How many points do you want to remove? "))

        if amount > my_character[attribute]:
            print("\nYou are taking away too many points!")
        else:
            my_character[attribute] -= amount
            my_character['points'] += amount
    else:
        print(
            "That attribute doesn't exist! \nYou might have to type it in all lowercase letters!!!")


def print_character():
    for attribute in my_character.keys():
        print("{} : {}".format(attribute, my_character[attribute]))


playContinue = "no"
while playContinue == "no":
    Continue = input("Are you sure you want to continue?\n")
    if Continue == "yes" or "Yes" or "y":
        playContinue = "yes"
        start_stat()
        add_character_points()
    elif Continue == "n" or "No" or "no":
        main()

running = True

while running:
    print("\nYou have {} points left\n".format(my_character['points']))
    print("1. Add points\n2. Remove points. \n3. See current attributes. \n4. Exit\n")

    choice = input("Choice: ")

    if choice == "1":
        add_character_points()
    elif choice == "2":
        remove_character_points()
    elif choice == "3":
        print_character()
    elif choice == "4":
        running = False
    else:
        pass


def story_str():
    print(
        "\nYou were always a strong child who easily do physical labor and gain lots of muscle."
)
    time.sleep(3)

    print("\nYou regularly trained with your dad who was also a skilled swordsman.")
    time.sleep(3)
    print("\nAs you grew into an adult, you're swordsmanship improved drastically.")
        time.sleep(3)
        print("\nOnce old enough, you joined the local guild as a warrior.")
        time.sleep(3)


def story_dex():
    print("\nYou were a sly child. You would always be stealing from other people and with"
    + "\nconstant practice you became proficient at thieving.")
    time.sleep(3)

    print("\nCombined with the skill of knives and short-blades, you became an extremely deadly assassin."
)
    time.sleep(3)

    print("\nOnce old enough, you joined the local guild as an assassin.")
    time.sleep(3)


def story_wis():
    print("\nYou grew up as a very intellegent child. You read books everyday and realized that magic"
    + "is the best offensively and defensively.")

    print("\nYou grew up and attended the best magic school avalible and graduated."
)

    print("\nYou soon went to the local guild and joined as a wizard.")


run_story = False

while run_story:
    if my_character['strength'] >= 13:
        story_str()
        chara_class = 'Warrior'
        run_story = True
    else:
        continue

    if my_character['dexterity'] >= 13:
        story_dex()
        chara_class = 'Assassin'
        run_story = True
    else:
        continue

    if my_character["wisdom"] >= 13:
        story_wis()
        chara_class = 'Mage'
        run_story = True
    else:
        continue

我在 part1 上输入的命令是尝试导入my_character:

from intro import my_character
print(my_character)

我一直在尝试导入my_character,但它出现了:

Traceback (most recent call last):
  File "C:/Users/user/Desktop/part1.py", line 5, in <module>
    my_character
NameError: name 'my_character' is not defined

原始文件名为“intro”,新文件名为“part1”。我是否需要执行'if name ==“__main ”'的事情?如果是这样,那会怎么做?

1 个答案:

答案 0 :(得分:0)

检查: 1)是文件名my_character.py 2)您已将其导入为my_character 3)my_character在主python目录中(如果你在解释器中导入)