Answers for "linear functions in python"

0

linear functions in python

def neg(formula: list):
    if formula[3] == "-":
        formula[4] = float("-" + str(formula[4]))
        formula[3] = "+"

def conv(lis: list) -> list:
    if len(lis) == 3:
        lis += ["+", "0"]
    lis[2] = float(lis[2])
    lis[4] = float(lis[4])
    return lis

def process(cmd: str, formula: list):
    cmd = cmd.split(" ")
    if len(cmd) == 3:
        if cmd == ["x", "=", cmd[2]]:
            neg(formula)
            print("y =", float(cmd[2]) * formula[2] + formula[4])
        elif cmd == ["y", "=", cmd[2]]:
            neg(formula)
            print("x =", (float(cmd[2]) - formula[4])/formula[2])
    elif cmd == ["y-intercept"]:
        print(formula[4])
    elif cmd == ["x-intercept"]:
        if formula[3] == "-":
            formula[4] = float("-" + str(formula[4]))
        print((0 - formula[4])/formula[2])
    elif cmd == ["run", cmd[1]]:
        for i in range(int(cmd[1])):
            print("(" + str(i) + ", " + str(i * formula[2] + formula[4]) + ")")


formula = conv(input("Type the formula:\n").replace("x", "").split(" "))
#Type your formula like this y = mx + b

print("\nCommands:")#The commands are x = value, y = value, y-intercept, and x-intercept to find your answer and run (value) how many times.
while True:
    process(input(), formula)
    print()
Posted by: Guest on September-18-2021

Python Answers by Framework

Browse Popular Code Answers by Language