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)
Paul Roub
36.5k27 gold badges88 silver badges95 bronze badges
asked Jan 21, 2016 at 2:24

1 Answer 1

1

Just assign and / or return quiz

def test_call(difficulty):
 quiz = test_method(difficulty)
 print quiz
 return quiz
answered Jan 21, 2016 at 2:28
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!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.