0
def title_sequence():
    title_file = open("title_main.txt", "r")
    all_title = title_file.read()
    print(all_title)
    title_file.close()

def title_option(title_option):
    VALID = ("new", "load", "exit")
    entry = " "
    print("Would you like to begin a new game, or load an old one?")
    while entry not in VALID:
        entry = input("Please enter either 'new' or 'load': ").lower()
        return entry   
def difficulty_sequence(difficulty):
    VALID1 = ("easy", "hard")
    difficulty = " "
    while difficulty not in VALID1:
        print("That is not a valid difficulty setting, please select enter either 'easy' or 'hard'.")
        difficulty = input("Would you like to play on easy or hard difficulty?: ").lower()
        return difficulty

import random
def easy_difficulty():
    health = 100
    xp = 100
    enemy_spawn = random.randrange(1,4)
    level_req = xp//10 + xp
    health_spawn = random.randrange(1,5)
    enemy_hit = random.randrange(3, 10)
    easy_difficulty = {"enemy_spawn" : enemy_spawn,
                       "level_req" : level_req,
                       "health_spawn" : health_spawn,
                       "enemy_hit" : enemy_hit}
     new_player = player.append(easy_difficulty)
     return new_player
    print(easy_difficulty)


def hard_difficulty():
    health = 100
    xp = 100
    enemy_spawn = random.randrange(1,4)
    level_req = xp//25 + xp
    health_spawn = random.randrange (1,7)
    enemy_hit = random.randrange(6,14)
    hard_difficulty = {"enemy_spawn" : enemy_spawn,
                       "level_req" : level_req,
                       "health_spawn" : health_spawn,
                       "enemy_hit" : enemy_hit}
     new_player = player.append(hard_difficulty)
     return new_player
     print(hard_difficulty)

def main():
    player = {"health" : 100,
              "xp" : 100,
              "strength" : 0,
              "dexterity" : 0,
              "wisdom" : 0,
              "enemy_spawn" : None,
              "level_req" : None,
              "health_spawn" : None,
              "enemy_hit" : None}
    choice = difficulty_sequence(difficulty)
    if choice == "easy":
        new_player = easy_difficulty()
        player = new_player
    elif choice == "hard":
        new_player = hard_difficulty()
        player = new_player

title_sequence()
title_option = title_option(title_option)
difficulty_sequence = difficulty_sequence(difficulty)

I'm trying to create this text based adventure game, but so far I've gotten hung up on calling variables to and from functions. Quite simply, this part of the code should display what title sequence I place in the file, ask the user which difficulty they would like to play on, then change the player stats in the main accordingly. The error occurs in the main I believe, was wondering if anyone could point me in the right direction. (Don't eat me I'm new here) Thanks!

Traceback (most recent call last):
  File "C:\Users\Harry\Desktop\Python\Project\new game test.py", line 83, in <module>
    main()
  File "C:\Users\Harry\Desktop\Python\Project\new game test.py", line 68, in main
    choice = difficulty_sequence(difficulty)
UnboundLocalError: local variable 'difficulty_sequence' referenced before assignment
3
  • 5
    Please show us the complete stacktrace. Commented Nov 25, 2013 at 12:24
  • 1
    That shouldn't happen. Are you sure the difficulty_sequence function is up there? Commented Nov 25, 2013 at 12:32
  • I even tested it myself, it doesn't have such error. Commented Nov 25, 2013 at 12:35

2 Answers 2

1

You haven't defined difficulty in main(). Where are you hoping to get it from?

In fact, since you already have the input in difficulty_sequence, you probably don't want to pass any parameters to that function, and just get it there.

Sign up to request clarification or add additional context in comments.

Comments

0

I see a couple of problems in the code you provide. However, none is related with the traceback.

First, difficulty is undefined. You don't even need this variable, and i think difficulty_sequence doesn't need an argument as well:

def difficulty_sequence():
    ...
    ...
def main():
    ...
    choice = difficulty_sequence()

Another one is that in your functions, player is undefined. Declare it at the top of your function.

def hard_difficulty():
    player = []

Also, there's an indentation error in the code you showed us here. Fixed one in the hard_difficulty:

hard_difficulty = {"enemy_spawn" : enemy_spawn,
                   "level_req" : level_req,
                   "health_spawn" : health_spawn,
                   "enemy_hit" : enemy_hit}
new_player = player.append(hard_difficulty)
return new_player

The other one is in easy_difficulty. Hope this helps!

1 Comment

I did thanks! The indentation was probably when I was spacing for this site =p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.