Changing a string into an already existing variable

Viewed 13

I have combined the letters and numbers of an already existing variable. I want to output what that variable is not just the string.

This is all the code but the problem is in the incorrect function

def correct():
    global score
    print("Correct")
    score=score+1
def incorrect():
    list=['a',str(Question_Number)]
    List=''.join(list)

    print("Incorrect it's",List )

def quiz():
    global Question_Number
    global score
    Question_Number=0
    score= 0
    q1=int(input("How many teams are in the premier league? "))
    answer1=20
    Question_Number=Question_Number+1
    if q1==20: 
        correct()
    elif q1!=20:
        incorrect()
quiz()

My aim is for if you get the question wrong it tells you by looking at the current question's answer.

1 Answers

I've noticed you haven't explicitly written your question is regarding Python code. This is fine, though for future questions, I'd imagine other contributors might find it more useful to know it beforehand.

As for the answer; You're currently defining the answer within the scope of the quiz function, meaning the function called "incorrect", can never see or know what the variable "answer1" is. You'll have to include the variable value as a parameter to the function, I edited your code, you may change it as you like.

def correct():
    global score
    print("Correct")
    score=score+1
def incorrect(correctAnswer):
    print("Incorrect it's", correctAnswer)

def quiz():
    global Question_Number
    global score
    Question_Number=0
    score= 0
    q1=int(input("How many teams are in the premier league? "))
    answer1=20
    Question_Number=Question_Number+1
    if q1==20: 
        correct()
    elif q1!=20:
        incorrect(answer1)
quiz()

Another solution would be to store the answers in a dictionary and having the "incorrect" function retrieve the answer by using the global variable "Question_Number", I feel this answer is a bit closer to your original strategy you had in mind. Good luck!

Related