在函数中调用和附加变量?

时间:2013-11-25 12:22:41

标签: python

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)

我正在尝试创建这个基于文本的冒险游戏,但到目前为止,我已经挂断了调用函数的变量。很简单,代码的这一部分应该显示我在文件中放置的标题序列,询问用户他们想要玩哪个难度,然后相应地改变主要的玩家统计数据。 错误发生在主要我相信,想知道是否有人能指出我正确的方向。 (不要吃我,我是新来的)谢谢!

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

2 个答案:

答案 0 :(得分:1)

您尚未在difficulty中定义main()。你希望从哪里得到它?

事实上,由于input中已经有difficulty_sequence,因此您可能不希望将任何参数传递给该函数,只需将其传递到那里。

答案 1 :(得分:0)

我在您提供的代码中看到了一些问题。但是,没有一个与回溯有关。

首先,difficulty未定义。你甚至不需要这个变量,我认为difficulty_sequence也不需要参数:

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

另一个问题是,在您的函数中,player未定义。将它声明在函数的顶部。

def hard_difficulty():
    player = []

此外,您在此处向我们展示的代码中存在缩进错误。修正了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

另一个在easy_difficulty。希望这有帮助!

相关问题