Forums

Python code not working possible problems : if/else

I am currently new to Python and am trying it out by making a code that's going to ask you different questions which are math related. This is how it's supposed to work : At first, you are asked to choose a level of the questions you're going to be asked. It's either easy, medium, or hard. Then, I get asked random questions for each level, and if I answer it right I'm going to be asked that question again, and if I answer it right, I get to move on and answer another question. I haven't yet done a medium or hard level, but they should be exactly the same as the easy level, just with different questions and answers. Some things I would like to add are to add a counter that gives you 10 seconds to answer each question and that after answering 10 questions the whole thing should be done. I also would like to make a pause between each question, but that's straight forward -- I just need to type "import time" at the beginning and put "time.sleep(x)" where x is the amount of time I wait.

The reason I haven't done all of this is because I am obviously having problems in the code. When you'll see it, you'll notice that in the easy level there are to questions but only two that are going to get randomized and the rest act as comments to keep things simple while testing. I haven't imported any modules other than import time and import random (idk if those count) and I want to keep the way I randomized my questions the same. Also, the only way of communication that occurs with the user and the code is by inputs, which I also want to keep the same. Since I want the questions to be randomized, I can't know which one is going to come up, and I cant assign an if function according to which random input will come out, so you'll notice that to confirm which random question comes up, I need to first type in what I'm supposed to calculate and then what the actual answer to the problem is. You'll also notice that I have many "prompts" which are for if the user answers something that isn't supposed to be typed, he'll be asked that question again.

The actual problem I have with the code is that when I am asked what is 2^2, I put 2^2 "to show that I am here" as it says in the code and then put in the actual answer. It then works correctly by saying that I'm right, however after that, it keeps putting "Put the expression that you're supposed to calculate to show that you are here" many times which is below my else function, so that code shouldn't intersect. Also, I've tried to make the user be asked another question after they answer one correctly, but I couldn't make it work. When I am asked "What is 3^2?" there's a slightly different story which should be the same as with when you're asked "What is 2^2?". Everything goes normal with me being asked "What is 3^2?", but then when I put in the expression I'm supposed to calculate to show that I'm here, it keeps saying that over and over again, not even letting me put in the final answer. I tried many times and don't know how to stop this. Since I'm new to Python, my code isn't in a file so you'll see it down below.

import time import random

def main():

def prompt1(): answer = \ input("Are you choosing an easy level, medium level, or hard level? Please respond with 'easy', 'medium', or 'hard'.") if (answer == "easy"): print("Loading easy test...") time.sleep(0.4)

def promptback(): Tup = \ ("What is 2^2? First put 2^2 to confirm you're here, then put the actual answer.", "What is 3^2? First put 3^2 to confirm you're here, then put the actual answer.") # ,"What is 4^2? First put 4^2 to confirm you're here, then put the actual answer.","What is 5^2? First put 5^2 to confirm you're here, then put the actual answer.","What is 6^2? First put 6^2 to confirm you're here, then put the actual answer.","What is 7^2? First put 7^2 to confirm you're here, then put the actual answer.","What is 8^2? First put 8^2 to confirm you're here, then put the actual answer.","What is 9^2? First put 9^2 to confirm you're here, then put the actual answer.","What is 10^2? First put 10^2 to confirm you're here, then put the actual answer.") list1 = list(Tup) random.shuffle(list1) answer2 = input(list1[1])

def prompt2(): if (answer2 == "2^2"):

def prompt3(): answer3 = input("Now put in the actual answer.") if (answer3 == 4): print ("You're right!") else: print("Try again.") prompt3()

prompt3() else:

print("Put in the expression you are supposed to calculate to show you are here.") prompt2()

prompt2()

def prompt4(): if answer2 == "3^2"):

def prompt5(): answer4 = input("Now put in the actual answer.") if answer4 == '9': print ("You're right!") else: print ("Try again.") prompt5()

prompt5() else: print("Put in the expression you are supposed to calculate to show you are here.") prompt4()

prompt4()

else: print ("Please respond with 'easy', 'medium', or 'hard'.") prompt1()

prompt1() main()

Hi Andrey, could you please insert your code once with preserving indentation. I see you've got nested function definitions and many if/else branches which without indentation become impossible to interpret.