0

I am attempting to create a program to solve a specific system of equations using brute force with python. I solved for one equation and substituted it into the second equation. Variables a through f are given by user input, and I am trying to output values for x and y within a range of -10 to 10 if they exist. Any help is greatly appreciated, Thank You!

My problem is that for each check within the range the output is no solution, whereas I would like it to output no solution only if none of the numbers within the range satisfy the equation. Updated the code to fix the int object not callable error.

To be clear, the code is mostly working and gives the correct answer, but the answer is nested in roughly 100 'no solution' statements


''' Read in first equation, ax + by = c '''
a = int(input())
b = int(input())
c = int(input())

''' Read in second equation, dx + ey = f '''
d = int(input())
e = int(input())
f = int(input())

for x in range(-10, 10, 1):
    for y in range(-10, 10, 1):
       if a * x + b * y == c and d * x + e * y == f:
           print(x, y)
       else:
           print('No solution')

# a * x + b * y = c
# d * x + e * y = f
Drkdeibs
  • 103
  • 1
  • 1
  • 8

5 Answers5

1

To fix the issue of the else statement printing for each iteration of the if statement I created a check and set it to false. I set a condition that set the variable to true if any of the numbers within the range satisfied the condition. Then set an if statement to print 'No solution' if the variable was still false. Thanks to everyone for the help with syntax, but I'm glad I had the opportunity to figure this out on my own. Here's the updated working code:

''' Read in first equation, ax + by = c '''
a = int(input())
b = int(input())
c = int(input())

''' Read in second equation, dx + ey = f '''
d = int(input())
e = int(input())
f = int(input())

check = False

for x in range(-10, 10, 1):
    for y in range(-10, 10, 1):
        if a * x + b * y == c and d * x + e * y == f:
            check = True
            print(x,y)

if check == False:
    print('No solution')

# a * x + b * y = c
# d * x + e * y = f
Drkdeibs
  • 103
  • 1
  • 1
  • 8
0
   if d * x + b((c - a * x)/b) == f:

Parentheses after a variable is an operator that tries to call the variable as a function. So this line above is trying to call b() as a function. You need to explicitly add the multiply operator instead:

   if d * x + b*((c - a * x)/b) == f:
Jeff
  • 1,234
  • 8
  • 16
  • Thank you. I realized this and fixed it. Also realized I can use an and statement so I don't have to use substitution and the code is more concise. – Drkdeibs Jun 08 '20 at 21:47
0

So, there is a lot going on here that can be improved upon. But with some basic assumptions on best practices I've re-written your code so that it is easy for anyone to understand.

The main things I'd recommend getting use to:

  1. Setting up a "main" in your python code
  2. building functions instead of relying on a procedural design
  3. commenting the goal of your functions, along with something about the input and output

There are plenty of discussion online as to why, but to sum them up it makes it A) more clear what your intentions are and B) provides an easier baseline for troubleshooting your code.

There are still more things that could be discussed about improving the clarity of intention and design but I'll cut myself short at fear of rambling on.


def brute_force_two_equations(first_values, second_values,
                              max_range=10, min_range=-10):
    '''
    Brute force way of finding solution in
    system of equations

    Params:
        first_values: a list of three values [a const, a const, and the result]
        second_values: a list of three values [a const, a const, and the result]
        max_range: the maximum range to test with, default 10
        min_range: the minimum range to test with, default -10
    '''
    for x in range(min_range, max_range + 1):
        for y in range(min_range, max_range + 1):
            first_result = equation(first_values[0], first_values[1], x, y)
            second_result = equation(second_values[0], second_values[1], x, y)
            if first_result == first_values[2] and second_result == second_values[2]:
                return (x, y)

def equation(const1, const2, x, y):
    '''
    A simple equation: a * x + b * y = c

    Params:
        const1: the first constant value, `a` in the above example
        const2: the second constant value, `b` in the above example
        x: the first variable value
        y: the second variable value
    '''
    return const1 * x + const2 * y

def get_three_integers():
    '''
    Requests and provides three integers from the user
    '''
    inputs = []
    for i in range(0, 3):
        inputs.append(int(input("Please enter a value: ")))
    return inputs

if __name__ == "__main__":
    print("== First Equation ==")
    first_equation_values = get_three_integers()
    print("== Second Equation ==")
    second_equation_values = get_three_integers()
    solution = brute_force_two_equations(first_equation_values,
                                         second_equation_values)
    if solution:
        print(f"(x, y) {solution}")
    else:
        print("No Solution found")



Taylor Cochran
  • 439
  • 4
  • 16
-1

You're saying "No solution" without complely looping through all the choices.

Code should be

for x in range(-10, 10, 1):
    for y in range(-10, 10, 1):
       if a * x + b * y == c and d * x + e * y == f:
           print(x, y)
else:
    print('No solution')
DarrylG
  • 16,732
  • 2
  • 17
  • 23
-1

This is the way I did it and it works: (I borrowed the check = False idea)

''' Read in first equation, ax + by = c '''
a = int(input())
b = int(input())
c = int(input())

''' Read in second equation, dx + ey = f '''
d = int(input())
e = int(input())
f = int(input())
check = False
for x in range(-10, 11):
    for y in range(-10, 11):
        if (a * x) + (b * y) != c:
            y += 1
            continue
        if (a * x) + (b * y) == c and (d * x) + (e * y) != f:
            y += 1
            continue
        if (a * x) + (b * y) == c and (d * x) + (e * y) == f:
            check = True
            print("x = {} , y = {}".format(x,y))
        else:
            x += 1
            continue
        
if check == False:
    print("There is no solution")
  • There are several issues with your solution. You shouldn't increment variables like `x` and `y` since they are looping variables and will be replaced by the next value anyhow. The only clause that's needed is the one that checks for the solution. – craigb Oct 30 '22 at 20:52