为什么我的变量设置为“当试图设置为包含字符串的另一个变量时?

时间:2017-09-24 19:33:17

标签: python-2.7

在理论上:当调用game_start并且用户输入'easy'时,变量quiz_paragraph应该假设值easy_paragprah

实际情况: quiz_paragraph的值设为"

我可以验证user_difficulty是否设置正确并正确'easy' set_difficulty ifquiz_paragraph语句运行但不会更改# Easy Paragraph easy_paragraph = '''\n___1___ is a programming ___2___ used to solve simple and complex problems. ___1___ like many languages can be used to display a message to a user using ___3___. Try it sometime. To display this common message by typing ___3___ '___4___ World!' \n''' # Init Variables user_difficulty = "" quiz_paragraph = "" # Ask user difficulty def ask_difficulty(): user_difficulty = raw_input("What difficulty level would you like? ") return user_difficulty # Difficulty sets returned Paragraph def set_difficulty(difficulty): if difficulty == "easy": quiz_paragraph = easy_paragraph return quiz_paragraph else: print '''Sorry, that is not a valid choice.''' # Start the game def game_start(): set_difficulty(ask_difficulty()) print quiz_paragraph # Game Start Test game_start() 的值。

谁能告诉我这里缺少什么?

bootstrap-select.js

1 个答案:

答案 0 :(得分:2)

看起来您的范围存在问题。

quiz_paragraph中的set_difficulty变量与'全球'不同。 quiz_paragraph。虽然它们目前使用相同的名称,但您可以将set_difficulty内的一个重命名为foo,但结果会相同。

您需要设置全局' quiz_paragraph返回set_difficulty返回的值,或game_start内更好的另一个局部变量。也就是说,

def game_start():
    quiz_paragraph = set_difficulty(ask_difficulty())
    print quiz_paragraph

def game_start():
    my_difficulty = set_difficulty(ask_difficulty())
    print my_difficulty