[SOLVED] Having trouble with Python: Multiple choice questions

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Hi there,
This is my first time programming python, so please don't judge.

So here is my code I want to use:
print("What is 10 to the power of 2?") words = ['1', '2000', '100', '10000000'] for w in range(len(words)): print(w, words[w]) w = int(input("Please type an answer: ")) if w == 100: print("YAY!!!") elif w == 1: print("NOPE XD") elif w == 2000: print("NOPE XD") elif w == 10000000: print("NOPE XD") else: print("Where u get that lol hehe") if w == 100: print("--------------------------------------------------------------------------------") print("Next Question!") print("""\ What is the discriminant formula? Hint: Type the number corresponding to the answer """) words = ["δ=b²-4ac", "δ=b²+4ac", "δ=4ac-b²", "4ac-b²+4ac=δ"] for w in range(len(words)): print(w, words[w]) w = int(input("Please enter a number: ")) if w == 0: print("Correct!") elif w == 1: print("Incorrect") elif w == 2: print("Incorrect") elif w == 3: print("Incorrect") else: print("Where u get that lol hehe")

It looks messy, but here is my question: I want to make it so that when the user inputs a wrong answer (such as 1) it goes back to the start of the question. And when they input a correct answer, i want it to go to the next question.

Is there anyway I can do that?

Thanks,
salm2s
 
Last edited:
Solution
I specifically defined the right answer as incorrect_answer = False, but it still returns with the same question

By default, any variable you point in a function in python, goes local, so in start1 function, when you set incorrect_answer = False, you are actually setting the local variable incorrect_answer , not the global one.

Fix is easy.
In start1 function, you should tell python you wish to change global variable, not local, so something like
Python:
def start1():
    global incorrect_answer; # now it points to global incorrect_answer  var, rather local
    ...

Hope it helps
You could put it all in a loop, which starts at 0 and increments until it hits one, and then subtract one if you submit a wrong answer.

As a side note: the segments of code such as:
elif w == 1:
print("Incorrect")
elif w == 2:
print("Incorrect")
elif w == 3:
print("Incorrect")

can be replaced with elif (w == 1 or w == 2 or w == 3) if I remember correctly (my memory of python syntax is not too great, but it's worth a shot anyways)
 

salm2s

Honorable
Jul 21, 2017
266
6
10,815
You could put it all in a loop, which starts at 0 and increments until it hits one, and then subtract one if you submit a wrong answer.

As a side note: the segments of code such as:
elif w == 1:
print("Incorrect")
elif w == 2:
print("Incorrect")
elif w == 3:
print("Incorrect")

can be replaced with elif (w == 1 or w == 2 or w == 3) if I remember correctly (my memory of python syntax is not too great, but it's worth a shot anyways)
How do i put it in a loop? Can you give an example? Sorry i am very new to coding
 

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Alright so I managed to get it to go back to start when wrong answer. Problem is that when it is right answer, it still goes back to the start. How can i make it continue?
Code:
incorrect_answer = True def start1(): print("What is 10 to the power of 2?") words = ['1', '2000', '100', '10000000'] for w in range(len(words)): print(w, words[w]) w = int(input("Please type an answer: ")) if w == 100: print("YAY!!!") incorrect_answer = False elif (w == 1 or w == 2000 or w == 10000000): print("NOPE XD") else: print("Where u get that lol hehe") while incorrect_answer: start1() print("--------------------------------------------------------------------------------") print("Next Question!") print("""\ What is the discriminant formula? Hint: Type the number corresponding to the answer """) words = ["δ=b²-4ac", "δ=b²+4ac", "δ=4ac-b²", "4ac-b²+4ac=δ"] for w in range(len(words)): print(w, words[w]) w = int(input("Please enter a number: ")) if w == 0: print("Correct!") elif (w == 1 or w == 2 or w == 3): print("Incorrect") else: print("Where u get that lol hehe")

I specifically defined the right answer as incorrect_answer = False, but it still returns with the same question
 
Sep 28, 2019
83
7
45
I specifically defined the right answer as incorrect_answer = False, but it still returns with the same question

By default, any variable you point in a function in python, goes local, so in start1 function, when you set incorrect_answer = False, you are actually setting the local variable incorrect_answer , not the global one.

Fix is easy.
In start1 function, you should tell python you wish to change global variable, not local, so something like
Python:
def start1():
    global incorrect_answer; # now it points to global incorrect_answer  var, rather local
    ...

Hope it helps
 
  • Like
Reactions: extreme_noob
Solution
By default, any variable you point in a function in python, goes local, so in start1 function, when you set incorrect_answer = False, you are actually setting the local variable incorrect_answer , not the global one.

Fix is easy.
In start1 function, you should tell python you wish to change global variable, not local, so something like
Python:
def start1():
    global incorrect_answer; # now it points to global incorrect_answer  var, rather local
    ...

Hope it helps

This looks right.

I mostly use java so I forgot about python's wierd local and global variable rules.
 

salm2s

Honorable
Jul 21, 2017
266
6
10,815
By default, any variable you point in a function in python, goes local, so in start1 function, when you set incorrect_answer = False, you are actually setting the local variable incorrect_answer , not the global one.

Fix is easy.
In start1 function, you should tell python you wish to change global variable, not local, so something like
Python:
def start1():
    global incorrect_answer; # now it points to global incorrect_answer  var, rather local
    ...

Hope it helps
Thanks this worked