-1

I'm trying to create a text-based game. I've made a basic/generic intro and now it's getting long so I want to create a new file so that I can add things like armor and weapons. When I went to try to import 'my_character' it gave me an error so I went to research this problem and I found this, so I tried it. I just got an error message saying it wasn't define (similar to the error below). This is the intro part named 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

The command I have typed on part1 is to try to import my_character is:

from intro import my_character
print(my_character)

I have been trying to import my_character but it comes up as:

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

The original file was named "intro" and the new one is named "part1". Do I need to do the 'if name == "__main"' thing? If so, what does that do?

Ender Kid
  • 13
  • 2
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Mar 30 '18 at 18:52
  • Please show the import statement – Susmit Mar 30 '18 at 18:55
  • You need to show your code for importing my_character. Up till now you've just talked about it, not shown it. It's unclear what relevance the code you've pasted has to your question and you've also asked two questions, which is confusing. – geoidesic Mar 30 '18 at 18:56
  • Your posted code doesn't run due to indentation errors. With those fixed, it requires manual input. We aren't going to slog through your intro dialogue. – Prune Mar 30 '18 at 18:58
  • You say the problem is importing `my_character`, but 1) there is no `import` statement for `my_character` and 2) the error message you show is about `print_character`. I recommend you reduce the code to a minimal, complete example that demonstrates the problem you are having, and then come back with that. – CAB Mar 30 '18 at 19:13
  • Yeah, my bad. I forgot to add that. – Ender Kid Mar 30 '18 at 19:18

1 Answers1

0

Check for: 1) is the file name my_character.py 2) you have imported it as my_character 3) my_character is in the main python directory (if you are importing in interpreter)

Susmit
  • 336
  • 1
  • 4
  • 12