d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Iso: Simple Calculator Quiz App
Add Reply New Topic New Poll
Member
Posts: 420
Joined: Apr 5 2022
Gold: 3.42
Apr 14 2023 04:14am
hi,
i need someone to help me to write a simple calculator quiz app

i need formula like"

1+2+3+4-3-4-5-x6x7/3/
basicly just random numbers 1-9 and random + - x /

make it up from 6 to 30ish length

please leave your comment or msg me thanks
Member
Posts: 3,638
Joined: Oct 10 2008
Gold: 1,405.36
May 2 2023 03:45pm
this is what you are looking for. Tip apreciate :) lmk if it work

import random

def generate_formula(length):
operators = ['+', '-', 'x', '/']
formula = str(random.randint(1, 9))
for i in range(length-1):
operator = random.choice(operators)
number = str(random.randint(1, 9))
formula += operator + number
return formula

def calculate_formula(formula):
return eval(formula)

length = random.randint(6, 30)
formula = generate_formula(length)
answer = calculate_formula(formula)

print("What is the answer to this formula?")
print(formula)
user_answer = input("Your answer: ")

if int(user_answer) == answer:
print("Correct!")
else:
print("Incorrect. The answer is", answer)



This code generates a random formula with a length between 6 to 30, using the four basic arithmetic operators (+, -, x, /) and random numbers between 1 to 9. It then evaluates the formula using Python's eval() function, and prompts the user to input their answer. If the user's answer matches the calculated answer, it prints "Correct!", otherwise it prints "Incorrect."



Member
Posts: 3,638
Joined: Oct 10 2008
Gold: 1,405.36
May 2 2023 03:46pm
this is for javascript

function generateFormula(length) {
const operators = ['+', '-', 'x', '/'];
let formula = '';
for (let i = 0; i < length; i++) {
if (i === 0) {
formula += Math.floor(Math.random() * 9) + 1; // start with a random number between 1-9
} else {
formula += operators[Math.floor(Math.random() * operators.length)]; // randomly select an operator
formula += Math.floor(Math.random() * 9) + 1; // add a random number between 1-9
}
}
return formula;
}

function evaluateFormula(formula) {
try {
return eval(formula); // evaluate the formula using JavaScript's eval() function
} catch (error) {
return 'Error: ' + error.message; // catch any errors that may occur during evaluation
}
}

const formulaLength = Math.floor(Math.random() * 25) + 6; // generate a random formula length between 6-30
const formula = generateFormula(formulaLength);
const answer = evaluateFormula(formula);

console.log('What is the answer to this formula?');
console.log(formula);
const userAnswer = prompt('Your answer: ');

if (userAnswer === answer.toString()) {
console.log('Correct!');
} else {
console.log('Incorrect. The answer is ' + answer);
}
Member
Posts: 4,848
Joined: Mar 1 2008
Gold: 5,064.33
May 4 2023 04:58am
if you need it in java and you need it to generate a random formula as a string (like "1+2-3/4+5*6")

Code
import java.util.*;

public class GenFormula {

private static final char[] ops = {'+','-','/','*'};
private static final Random rand = new Random();

public static void main(String[] args) {
System.out.println(genFormulaStr(rand.nextInt(2,20)));
}

private static String genFormulaStr(int maxNum){
StringBuilder res = new StringBuilder();
for (int i = 1; i <= maxNum; i++) {
res.append(i);
res.append(i == maxNum ? "" : ops[rand.nextInt(4)]);
}
return res.toString();
}
}


Go Back To Programming & Development Topic List
Add Reply New Topic New Poll