-1

I am working on a program to build a quiz. I need to get my "true" out of the first loop in order to use it for the second.

As it is right now, Python says that "true" is not defined.

def verif(): #this is the function to check if answer was right
    y=(chosen_answer.get())
    true=0
    if chosen_answer==1:
       true=true+1
    else:
        true=true  #at the end true represents the number of correct answers
    return true 
def final_count():
    if true==2:   #here it shows that "true" was not identified
        print("All of the answers were answered correctly!")
    elif true==1:
        print("50% were answered correctly")
    else:
        print("none of the answers were right..")
Elise
  • 1
  • 1
  • 1
    You don't have any loops, do you mean to be referring to the `verif` and `final_count` functions? – Francesco Gramano May 03 '15 at 07:59
  • generally, using a language construct like `true` as a variable name is a bad idea. `final_count` is a completely separate function from `verif`, so the variables can't be shared like that. – Huey May 03 '15 at 07:59
  • indeed, excuse me, i was referring to the functions. Is there any way I could share them? @Huey – Elise May 03 '15 at 08:04
  • You can use a global variable to share between functions – Kakshil Shah May 03 '15 at 08:04
  • @Huey `true` is not a language construct in any version of Python, fyi. But your point is still valid, though. – kqr May 03 '15 at 08:05
  • Probably better to let `verif` return `trueVar`, then use it as a parameter for `final_count`. @kqr, that's `True`, I forgot python booleans are capitalised. – Huey May 03 '15 at 08:06
  • possible duplicate of [UnboundLocalError in Python](http://stackoverflow.com/questions/9264763/unboundlocalerror-in-python) – Łukasz Rogalski May 03 '15 at 08:07
  • It worked by using a global variable! Thank you! @KakshilShah – Elise May 03 '15 at 08:12
  • You generally should not use keywords as variables for clarity and to avoid conflict. Try isTrue or answerIsTrue. Your question would be more understandable if you crafted it better. You have no loops. You seem to have a case where a recursive function could be handy which could act like a loop across your questions-answers set. Without more context, it is unclear how you are going to implement this dynamically. You can use global variables/class properties to track values between functions. You should also consider abstracting your check values which are currently "magic numbers". – williambq May 03 '15 at 08:13
  • You can mark the answer correct and close the questions for future use. – Kakshil Shah May 03 '15 at 08:13

1 Answers1

1

Pass is as an argument:

def final_count(true):
    if true==2:   
        print("All of the answers were answered correctly!")
    elif true==1:
        print("50% were answered correctly")
    else:
        print("none of the answers were right..")

since you return it in the verif function, you can simply run

true = verif()
final_count(true)

I recommend changing the variable name from true to, say count or basically anything else. Also, when dealing with local variables, you don't have to keep the same name

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Matthew
  • 672
  • 4
  • 11