0

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)

1 Answer 1

1

Just assign and / or return quiz

def test_call(difficulty):
    quiz = test_method(difficulty)
    print quiz
    return quiz
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you BenJ! That did the trick - your help is much appreciated!
@Aaron Glad I could help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.