Home > Back-end >  program that ask consecutive math questions
program that ask consecutive math questions

Time:11-04

I need to make a program that asks you to answer a series of math questions. I'm able to make it ask random operations and the numbers for the operation, but I want to make it ask numerous questions in one run. I think I need to make a loop, but I don't know how to do that. I basically want it to ask as many questions until the user stops, and I want it to count the score, and how many attempts were made. It's in French, so bear with me.

#-Importation des modules
import random


#Define operations
Operation = ("Addition", "Soustraction", "Multiplication", "Division", "Exposant", "Pente", "Point de Millieu")
Question = random.choice(Operation) #Chose random operation
score = 0 #Everytime you get a right answer, add a point
#Les questions

#Addition
if Question == "Addition":
    num1 = random.randint(10,100)
    num2 = random.randint(50,200)
    BnReponse = num1   num2 #Right answer
    print(f"Trouve la somme de {num1} {num2}")
    ReponseUs = int(input(f"C'est quoi la somme de {num1} {num2} est: ")) #User's answer
    if ReponseUs == BnReponse:
        print("Correct")
        score =1
        print(f"Le score est: {score}")
    else:
        print(f"Incorrect, la reponse est {BnReponse}")

#Soustraction
elif Question == "Soustraction":
    num1 = random.randint(100,200)
    num2 = random.randint(10,80)
    BnReponse = num1 - num2 #La bonne reponse
    print(f"Trouve la difference de {num1}-{num2}")
    ReponseUs = int(input(f"C'est quoi la difference de {num1}-{num2} est: "))
    if ReponseUs == BnReponse:
        print("Correct")
        score =1
    else:
        print(f"Incorrect, la reponse est {BnReponse}")

#Multiplication
elif Question == "Multiplication":
    num1 = random.randint(10,20)
    num2 = random.randint(10,20)
    BnReponse = num1 * num2 #La bonne reponse
    print(f"Trouve le quotient de {num1}*{num2}")
    ReponseUs = int(input(f"C'est quoi le quotient de {num1}-{num2} est: "))
    if ReponseUs == BnReponse:
        print("Correct")
        score =1
    else:
        print(f"Incorrect, la reponse est {BnReponse}")

CodePudding user response:

For the loop, you can do something like this if you want an infinite loop:

while True:
    ...

or if you want a numerous loop (10 times for the example):

for i in range(10):
    ...

for this, i equal the number of the question (i=0 for the first, i=1 for the second...). To stop the loop, you can also use break in your code like this:

while True:
    question = input('')
    if question == 'stop':
        break

And to count the scores, just do

score  = 1

each time the player wins, and print it at the end :)

CodePudding user response:

I'm able to make it ask random operations and the numbers for the operation, but I want to make it ask numerous questions in one run. I think I need to make a loop, but I don't know how to do that.

Depends on what kind of behavior you want for the loop but the one that jumps out to me for your case is a while loop. You can make a variable and set it to true and then when the user decides to stop then set that variable to false. And you can count the number of iterations with a counter variable.

counter_var = 0
loop_var = True    
while loop_var:
    counter_var  = 1
    # your code to ask questions goes in here

At the end of the loop you can ask the user whether or not they want to continue with input and based on that set loop_var to false.

CodePudding user response:

You will need to add a loop so it would look a bit like:

#-Importation des modules

import random

#Define operations
score = 0 #Everytime you get a right answer, add a point
Operation = ("Addition", "Soustraction", "Multiplication", "Division", "Exposant", "Pente", "Point de Millieu")
game = True
turns =0
#Les questions
while game:
    turns   
    Question = random.choice(Operation) #Chose random operation
    #Here you would insert your game code
    if (input("Do you want to stop playing Y/N") == "Y"): #detecting if they said Y
         game = False
         print("You scored: "   score   " after: "   turns   " attempts")

CodePudding user response:

I don't want to give the solution away as it is for a homework assignment but hopefully, this will be a good start to help with your issue.

I just added a while loop to your code and showed a possible way you could exit the loop using the wantQuestion boolean and asking the user if they want another question after every question.

#Importation des modules
import random

#Define operations
Operation = ("Addition", "Soustraction", "Multiplication") #Add more operations
score = 0 #Everytime you get a right answer, add a point
wantQuestion = True #True means the user wants another question

while (wantQuestion): #runs while wantQuestion is True
    Question = random.choice(Operation) #Chose random operation
    #Les questions
    if Question == "Addition":
        print("Addition Code runs")
    elif Question == "Soustraction":
        print("Soustraction Code runs")
    elif Question == "Multiplication":
        print("Multiplication Code runs")
    # Other operations

    keyboardInput = input("Would you like another question (Y/n): ")
    if (keyboardInput == "y" or keyboardInput == "Y"):
        wantQuestion = True
    else:
        wantQuestion = False

print("Display what you want after you've stopped asking questions")
  • Related