[Python] Creating code which generates random mathematical problem for user to solve. Can't...

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    Just learned a bit of Python language. While having fun with Python I got stuck on algorithm problem. Couldn't figure out how to make mathematical notations, stored in array as strings, as real mathematical processes (adding, subtracting, etc.).

    Also, mathematical notations should follow the order of operations as in math (multiplication/division, addition/subtraction). Could somebody try to write an algorithm in section (NOT FINISHED!) without changing overall code. Variable problem_solved should have the answer for the generated problem.

    Would be very grateful. Maybe this site has brilliant minds.

    If it's not possible due to a fact how this code is written, let me know :). I'm eager to learn.

    Code purpose: generate random mathematical problem for user to solve.

    from random import randint

    #constants
    operators = ["+","-","*","/"]

    #variable for program to continue
    user_proceed = True

    #asking user for the maximum amount of numbers
    val = False
    while val == False:
    try:
    count_max = int(input("Maximum amount of numbers: "))
    if(count_max < 2):
    print("Has to be greater than 1. ", end = '')
    else:
    #limits user's ability to pick infinite amount of numbers
    if(count_max > 20):
    print("Hold your horses. Pick something a bit easier. ", end = '')
    else:
    val = True
    except ValueError:
    print("That's not a valid number! ", end = '')

    #program will repeat if procced will be true
    while user_proceed == True:

    #generating the count of numbers and operators
    count_num = randint(2,count_max)
    count_opp = count_num - 1

    #creating arrays for numbers and operators
    num = [None] * count_num
    opp = [None] * count_opp

    #generating random numbers for problem
    i = -1
    for x in num:
    i = i+1
    if x == None:
    num = randint(1,20)
    #print(numbers) #for testing purpose

    #generating problem for user to solve
    problem_str = ""
    i = -1
    for x in num:
    i = i+1
    if i == count_opp:
    problem_str = problem_str + str(num)
    else:
    opp = operators[randint(0,3)]
    problem_str = problem_str + str(num) + opp
    #print(opp) #for testing purpose
    #print(problem_str) #for testing purpose


    #calculating solution to the problem (NOT FINISHED!)
    problem_solved = 0

    #asking user for his solution
    print(f"Solve this problem: {problem_str}")
    val = False
    while val == False:
    try:
    user_solution = float(input("Answer: "))
    val = True
    except ValueError:
    print("That's not a valid number! ", end = '')

    #checking if user got the right answer
    if user_solution == problem_solved:
    print("Correct!")
    else:
    print(f"Wrong! The answer is {problem_solved}")

    #asking user to try again
    print("Try again? ", end='')
    val = False
    while val == False:
    user_answer = input("Yes/No: ")
    if(user_answer != "Yes" and user_answer != "No"):
    print("I don't understand. ", end = '')
    else:
    val = True
    if user_answer == "Yes":
    user_proceed = True
    else:
    user_proceed = False

    Continue reading...

Compartilhe esta Página