-3
name = input("Please enter your name: ")







is not defined on line 15. Is there a way to make this work? I tried switching this line of code:To the bottom but then it does not prompth the user if they want to play.

Is there also a way i can prompth the user what subject they want to get tested on, then it will call the function to that subject and prompth the user with questions regarding that subject.

Alex mos
  • 1
  • 1

2 Answers2

-1

You're accessing variables/functions that are not defined or not within the scope where they are being accessed.

  1. You need to define the math_Questions() function before you call it, that means moving it above the while loop at the top level.

  2. Additionally, the variables incorrect and correct are not accessed within your function as they are not within the scope of the function. You can either create new variables inside the function or declare them as global variables at the top level, the former is the more preferred approach as you should avoid global variables where possible as it reduces the modularity of your code.

Working example

def math_Questions():
  incorrect = 0
  correct = 0
  ans1 = input("Question 1: Ron had 6 bags of flour but used up 4 bags on Sunday. How many bags of flour did he have left?: ")
  if ans1 == "2":
      print("\nCorrect! " + ans1 + " is the right answer.")
      correct += 1
  else:
      print("\nYou are way off. Start from the very beginning, or continue with the next one.")
      incorrect += 1
  print("")
  print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

  print("")
  ans2 = input("Question 2: The Miller parking lot was full at the capacity of 15 this morning, now at the end of the day, 12 left the lot. How many cars are left in the parking lot?: ")
  if ans2 == "3":
      print("\nCorrect! " + ans2 + " is the right answer.")
      correct += 1
  else:
      print("\nYou are way off. Start from the very beginning, or continue with the next one.")
  print("")
  print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

  ans3 = input("Question 3: Mrs. Sheridan has 11 cats. How many more cats does Mrs. Sheridan need to have 43 cats?: ")
  if ans3 == "32":
      print("\nCorrect! " + ans3 + " is the right answer.")
      correct += 1
  else:
      print("\nYou are way off. Start from the very beginning, or continue with the next one.")
  print("")
  print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

  ans4 = input("Shelly bought a bed sheet for $1699. She gave the shopkeeper $2000. What amount will be returned by the shopkeeper?: ")
  if ans4 == "301":
      print("\nCorrect! " + ans4 + " is the right answer.")
      correct += 1
  else:
      print("\nYou are way off. Start from the very beginning, or continue with the next one.")
  print("")
  print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))
  
  return (correct, incorrect)

name = input("Please enter your name: ")
print("")
run = int(input("Hello " + name + ", would you like to play a game? (Enter 1 to continue, 2 to exit): "))
print("")

while run == 1:
  print("I will be asking a series of questions in each categories: mathematical, scientifical. Each question getting harder.")
  correct, incorrect = math_Questions()
  print("correct:", correct, ", incorrect: ", incorrect)

As for prompting the user for the subjects they want to get tested on, you can just make new functions for each subject and call based on the subject the user selects:

if (subject == "chemistry"):
    chem_questions()

I would also suggest storing the question and answer pairs within a dict or json object to make the code more readable and the program more modular and scalable.

  • Within a source code file, it is not necessary - in general - to put definitions earlier in the file than the code calling them. (There are no "forward declarations" in Python, so this would be impossible with functions that call each other.) Rather, the definition has to happen **before the code runs** that calls the function. However, because the `while` loop is **at top level**, it will run before the function is defined. – Karl Knechtel Jan 22 '23 at 06:04
-1

Firstly, you need to properly format your code for a start. Correct indents are important in python. Each indent should be exactly 4 spaces, no more, no less. It also makes your code much easier to read.

Secondly, you should declare your methods and classes first, then your code to call those methods.

Thirdly, you need to be careful of initialising variables that then get called in the function (ie correct = 0 and incorrect = 0). This is generally bad practice. In order to work they need to be re-declared in method as global. In your case, just declare them in method ONLY. Your code should look more like:

def math_questions():
    correct = 0
    incorrect = 0

    ans1 = input("Question 1: Ron had 6 bags of flour but used up 4 bags on Sunday. How many bags of flour did he have left?: ")
    if ans1 == "2":
        print("\nCorrect! " + ans1 + " is the right answer.")
        correct += 1
    else:
        print("\nYou are way off. Start from the very beginning, or continue with the next one.")
        incorrect += 1
        print("")
        print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

        print("")
    ans2 = input("Question 2: The Miller parking lot was full at the capacity of 15 this morning, now at the end of the day, 12 left the lot. How many cars are left in the parking lot?: ")
    if ans2 == "3":
        print("\nCorrect! " + ans2 + " is the right answer.")
        correct += 1
    else:
        print("\nYou are way off. Start from the very beginning, or continue with the next one.")
        print("")
        print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

    ans3 = input("Question 3: Mrs. Sheridan has 11 cats. How many more cats does Mrs. Sheridan need to have 43 cats?: ")
    if ans3 == "32":
        print("\nCorrect! " + ans3 + " is the right answer.")
        correct += 1
    else:
        print("\nYou are way off. Start from the very beginning, or continue with the next one.")
        print("")
        print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))

    ans4 = input("Shelly bought a bed sheet for $1699. She gave the shopkeeper $2000. What amount will be returned by the shopkeeper?: ")
    if ans4 == "301":
        print("\nCorrect! " + ans4 + " is the right answer.")
        correct += 1
    else:
        print("\nYou are way off. Start from the very beginning, or continue with the next one.")
        print("")
        print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))


name = input("Please enter your name: ")
print("")
run = int(input("Hello " + name + ", would you like to play a game? (Enter 1 to continue, 2 to exit): "))
print("")

while run == 1:
    print("I will be asking a series of questions in each categories: mathematical, scientifical. Each question getting harder.")
    math_questions()

Furthermore, I would recommend breaking down your code into smaller chunks and re-use code where possible. All your if statements have the same format and lend themselves to being put into another function or some loop. Look at:

def question_answered_correctly(new_question, correct_answer):
    ans = input(new_question)
    return ans == correct_answer:


def math_questions():
    correct = 0
    incorrect = 0

    question_list = [
        {
            'question': "Question 1: Ron had 6 bags of flour but used up 4 bags on Sunday. How many bags of flour did he have left?: ",
            'answer': '2'},
        {
            'question': "Question 2: The Miller parking lot was full at the capacity of 15 this morning, now at the end of the day, 12 left the lot. How many cars are left in the parking lot?: ",
            'answer': '3'},
        {
            'question': "Question 3: Mrs. Sheridan has 11 cats. How many more cats does Mrs. Sheridan need to have 43 cats?: ",
            'answer': '32'},
        {
            'question': "Question 4: Shelly bought a bed sheet for $1699. She gave the shopkeeper $2000. What amount will be returned by the shopkeeper?: ",
            'answer': '301'}
    ]

    for question in question_list:
        if question_answered_correctly(question['question'], question['answer']):
            print("\nCorrect! " + question['answer'] + " is the right answer.")
            correct += 1
        else:
            print("\nYou are way off. Start from the very beginning, or continue with the next one.")
            incorrect += 1
        print("")
        print('So far, you answered correctly to {0} questions and incorrectly to {1}. Keep Going!'.format(correct, incorrect))


name = input("Please enter your name: ")
print("")
run = int(input("Hello " + name + ", would you like to play a game? (Enter 1 to continue, 2 to exit): "))
print("")

while run == 1:
    print("I will be asking a series of questions in each categories: mathematical, scientifical. Each question getting harder.")
    math_questions()

Galo do Leste
  • 703
  • 5
  • 13