Aaron
Aaron

Reputation: 3

Passing a variable between Python functions

I am new to Python and coding and am trying to figure out how to pass the result of one function to a calling function so that the variable passed can be used within the calling function.

In the code below, I get an error that states that:

global name 'quiz' is not defined.

My intent is to pass back a string to be printed based upon user input.

difficulty = raw_input("Please enter the level of difficulty: easy, medium, or hard: ")

def test_method(difficulty):
  if difficulty == 'easy':
    quiz = "Yup - quiz me"
  else:
    quiz = "Nope - yikes!"
  return quiz

I've tried using print quiz here and the expected string prints. But what I would really like to do is pass back the quiz variable to the calling function and then print the result from there.

def test_call(difficulty):
  test_method(difficulty)
  print quiz

test_call(difficulty)

Upvotes: 0

Views: 61

Answers (1)

BenJ
BenJ

Reputation: 456

Just assign and / or return quiz

def test_call(difficulty):
    quiz = test_method(difficulty)
    print quiz
    return quiz

Upvotes: 1

Related Questions