0

In Theory: When game_start is called and the user inputs 'easy' the variable quiz_paragraph should assume the value of easy_paragprah

In Actuality: The value of quiz_paragraph is set to "

I can verify that user_difficulty is being set properly and is feeding 'easy' properly into set_difficulty. The if statement runs but doesn't alter the value of quiz_paragraph.

Can anyone tell me what I am missing here?

# 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()   
yohohosilver
  • 367
  • 1
  • 2
  • 15
  • Why is it people downvote questions with zero reasons given? You would think they would want us to learn what we did wrong? – yohohosilver Sep 24 '17 at 19:53
  • 3
    Not your down-voter, but 1) get rid of the apologies paragraph as it adds no information that would be helpful to future visitors with similar problems or to folks who might answer your question. 2) your question could be improved by showing the concrete results obtained from your searches of similar questions on this and other sites. I'm not talking about throw away lines such as "I searched everywhere and found nothing" but again ***concrete*** results. This site expects that you will do an exhaustive search *prior* to asking, and has high standards. – Hovercraft Full Of Eels Sep 24 '17 at 19:57
  • 3
    Also no one is required to comment when posting up-votes or down-votes as that often leads to little but flame wars. Check the [meta site](https://meta.stackoverflow.com/questions/261173/how-do-we-avoid-downvotes-without-a-comment) for more discussions on this topic as it gets asked a lot – Hovercraft Full Of Eels Sep 24 '17 at 20:01
  • 1
    Thank you for the clarifications. – yohohosilver Sep 24 '17 at 21:18

1 Answers1

2

It looks like you have an issue with scope.

The quiz_paragraph variable within set_difficulty, is not the same as the 'global' quiz_paragraph. Although they currently share the same name, you could rename the one inside set_difficulty to foo and you would have the same result.

You need to set the 'global' quiz_paragraph to the value returned by set_difficulty, or a better another local variable within game_start. That is,

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

or

def game_start():
    my_difficulty = set_difficulty(ask_difficulty())
    print my_difficulty
jhenderson2099
  • 956
  • 8
  • 17
  • Thank you. I didn't realize that Global/Local was a factor. Just as a quick question to make sure I understand. So a variable that is more than a single "level" deep in a function becomes a local variable? So if I want to change a global variable I have to do it in the first "tier" of a function/loop/etc. – yohohosilver Sep 24 '17 at 19:51
  • 1
    Correct. Check out https://stackoverflow.com/a/292502/1290118 for a quick run-down of the rules. – jhenderson2099 Sep 24 '17 at 19:54
  • Thank you again. Appreciate you helping out the noob. Reading up on it now. – yohohosilver Sep 24 '17 at 19:55