Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple solver
- import os
- os.system("clear")
- print("Simple Solver")
- print("Enter a problem in the following format using integers only: ")
- print("1 2 3 == 7")
- print("Type q to quit")
- def solver(x, y, z, answer):
- # Definte list of possible operators
- operations = ["+", "-", "*", "/", "%"]
- # Assume no solution found
- solution_found = False;
- # Iterate through all possibilities
- for o1 in operations:
- for o2 in operations:
- equation = "{} {} {} {} {}".format(x, o1, y, o2, z)
- if eval(equation) == answer:
- print("\n{} = {}".format(equation, answer))
- solution_found = True
- # Check if solution not found
- if not solution_found:
- print("No solution found")
- while True:
- # Get the problem from the user
- problem = input("\n> ")
- # Split into tokens
- tokens = problem.split()
- if tokens[0] == "q":
- break
- try:
- # Convert to integers
- x = int(tokens[0])
- y = int(tokens[1])
- z = int(tokens[2])
- answer = int(tokens[-1])
- except:
- print("Invalid input")
- continue
- solver(x, y, z, answer)
- print("\nThe end")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement