Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:TypeError:无法解压缩不可iterable的非类型对象_Python_Matplotlib_Sympy - Fatal编程技术网

python:TypeError:无法解压缩不可iterable的非类型对象

python:TypeError:无法解压缩不可iterable的非类型对象,python,matplotlib,sympy,Python,Matplotlib,Sympy,嘿,所以我在做一个项目,我为一个给定的线性方程绘制一个图表 我遇到了这个错误: import matplotlib.pyplot as plt import sympy as simp import re ''' Linear Eq formula = y = ax + c ''' #eq1 = a1x + b1y + c1 = 0 #eq2 = a2x + b2y + c2 = 0 def a_inp_eq1(): global a1 a1 = input('Plea

嘿,所以我在做一个项目,我为一个给定的线性方程绘制一个图表

我遇到了这个错误:

import matplotlib.pyplot as plt
import sympy as simp
import re


'''
Linear Eq formula = y = ax + c 
'''

#eq1 = a1x + b1y + c1 = 0
#eq2 = a2x + b2y + c2 = 0

def a_inp_eq1():
    global a1
    a1 = input('Please specify the value of "a" for the first equation: ')
    a1 = simp.sympify(a1)

    global b1
    b1 = input('Please specify the value of "b" for the first equation: ')
    b1 = simp.sympify(b1)

    global c1
    c1 = input('Please specify the value of "c" for the first equation: ')
    c1 = simp.sympify(c1)

def a_inp_eq2():
    global a2
    a2 = input('Please specify the value of "a" for the second equation: ')
    a2 = simp.sympify(a2)

    global b2
    b2 = input('Please specify the value of "b" for the second equation: ')
    b2 = simp.sympify(b2)

    global c2
    c2 = input('Please specify the value of "c" for the second equation: ')
    c2 = simp.sympify(c2)

def check_errors_eq1():
    try:
        int(a1)
        int(b1)
        int(c1)
    except ValueError:
        try:
            float(a1)
            float(b1)
            float(c1)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq1()

def check_errors_eq2():
    try:
        int(a2)
        int(b2)
        int(c2)
    except ValueError:
        try:
            float(a2)
            float(b2)
            float(c2)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq2()



def conversion():
    global a_con1
    a_con1 = a1 * -1
    global c_con1
    c_con1 = c1 * -1

    global a_con2
    a_con2 = a2 * -1
    global c_con2
    c_con2 = c2 * -1


# equation solver
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^{x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)


# defining the main plotting function
def Linear_eq(a,b,clr):
    x = list(range(0,11))
    y = [(a * ints + b) for ints in x]
    print(y)
    plt.plot(x , y ,label = 'linear' ,linestyle = '-' ,color= clr)

def plot():
    solx , soly = solve_eq()
    Linear_eq(a_con1,c_con2,'r')
    Linear_eq(a_con2,c_con2,'b')
    plt.annotate('(%s,%s)'%(solx,soly) , (float(solx),float(soly)))
    plt.show()

def run():
    a_inp_eq1()
    a_inp_eq2()
    check_errors_eq1()
    check_errors_eq2()
    conversion()
    plot()

run()
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^ {x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)
TypeError:无法解压缩不可编辑的非类型对象

完全回溯错误:

import matplotlib.pyplot as plt
import sympy as simp
import re


'''
Linear Eq formula = y = ax + c 
'''

#eq1 = a1x + b1y + c1 = 0
#eq2 = a2x + b2y + c2 = 0

def a_inp_eq1():
    global a1
    a1 = input('Please specify the value of "a" for the first equation: ')
    a1 = simp.sympify(a1)

    global b1
    b1 = input('Please specify the value of "b" for the first equation: ')
    b1 = simp.sympify(b1)

    global c1
    c1 = input('Please specify the value of "c" for the first equation: ')
    c1 = simp.sympify(c1)

def a_inp_eq2():
    global a2
    a2 = input('Please specify the value of "a" for the second equation: ')
    a2 = simp.sympify(a2)

    global b2
    b2 = input('Please specify the value of "b" for the second equation: ')
    b2 = simp.sympify(b2)

    global c2
    c2 = input('Please specify the value of "c" for the second equation: ')
    c2 = simp.sympify(c2)

def check_errors_eq1():
    try:
        int(a1)
        int(b1)
        int(c1)
    except ValueError:
        try:
            float(a1)
            float(b1)
            float(c1)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq1()

def check_errors_eq2():
    try:
        int(a2)
        int(b2)
        int(c2)
    except ValueError:
        try:
            float(a2)
            float(b2)
            float(c2)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq2()



def conversion():
    global a_con1
    a_con1 = a1 * -1
    global c_con1
    c_con1 = c1 * -1

    global a_con2
    a_con2 = a2 * -1
    global c_con2
    c_con2 = c2 * -1


# equation solver
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^{x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)


# defining the main plotting function
def Linear_eq(a,b,clr):
    x = list(range(0,11))
    y = [(a * ints + b) for ints in x]
    print(y)
    plt.plot(x , y ,label = 'linear' ,linestyle = '-' ,color= clr)

def plot():
    solx , soly = solve_eq()
    Linear_eq(a_con1,c_con2,'r')
    Linear_eq(a_con2,c_con2,'b')
    plt.annotate('(%s,%s)'%(solx,soly) , (float(solx),float(soly)))
    plt.show()

def run():
    a_inp_eq1()
    a_inp_eq2()
    check_errors_eq1()
    check_errors_eq2()
    conversion()
    plot()

run()
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^ {x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)
回溯(最近一次呼叫最后一次):

第116行,在 运行()

第114行,运行中 绘图()

第102行,在绘图中 solx,soly=solve_eq()

TypeError:无法解压缩不可编辑的非类型对象

代码:

import matplotlib.pyplot as plt
import sympy as simp
import re


'''
Linear Eq formula = y = ax + c 
'''

#eq1 = a1x + b1y + c1 = 0
#eq2 = a2x + b2y + c2 = 0

def a_inp_eq1():
    global a1
    a1 = input('Please specify the value of "a" for the first equation: ')
    a1 = simp.sympify(a1)

    global b1
    b1 = input('Please specify the value of "b" for the first equation: ')
    b1 = simp.sympify(b1)

    global c1
    c1 = input('Please specify the value of "c" for the first equation: ')
    c1 = simp.sympify(c1)

def a_inp_eq2():
    global a2
    a2 = input('Please specify the value of "a" for the second equation: ')
    a2 = simp.sympify(a2)

    global b2
    b2 = input('Please specify the value of "b" for the second equation: ')
    b2 = simp.sympify(b2)

    global c2
    c2 = input('Please specify the value of "c" for the second equation: ')
    c2 = simp.sympify(c2)

def check_errors_eq1():
    try:
        int(a1)
        int(b1)
        int(c1)
    except ValueError:
        try:
            float(a1)
            float(b1)
            float(c1)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq1()

def check_errors_eq2():
    try:
        int(a2)
        int(b2)
        int(c2)
    except ValueError:
        try:
            float(a2)
            float(b2)
            float(c2)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq2()



def conversion():
    global a_con1
    a_con1 = a1 * -1
    global c_con1
    c_con1 = c1 * -1

    global a_con2
    a_con2 = a2 * -1
    global c_con2
    c_con2 = c2 * -1


# equation solver
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^{x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)


# defining the main plotting function
def Linear_eq(a,b,clr):
    x = list(range(0,11))
    y = [(a * ints + b) for ints in x]
    print(y)
    plt.plot(x , y ,label = 'linear' ,linestyle = '-' ,color= clr)

def plot():
    solx , soly = solve_eq()
    Linear_eq(a_con1,c_con2,'r')
    Linear_eq(a_con2,c_con2,'b')
    plt.annotate('(%s,%s)'%(solx,soly) , (float(solx),float(soly)))
    plt.show()

def run():
    a_inp_eq1()
    a_inp_eq2()
    check_errors_eq1()
    check_errors_eq2()
    conversion()
    plot()

run()
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^ {x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)
我想问题出在我的正则表达式上,但我不知道如何解决它

这是我试图匹配的输出: {x:-3,y:-2}

这是正在执行此操作的函数:

import matplotlib.pyplot as plt
import sympy as simp
import re


'''
Linear Eq formula = y = ax + c 
'''

#eq1 = a1x + b1y + c1 = 0
#eq2 = a2x + b2y + c2 = 0

def a_inp_eq1():
    global a1
    a1 = input('Please specify the value of "a" for the first equation: ')
    a1 = simp.sympify(a1)

    global b1
    b1 = input('Please specify the value of "b" for the first equation: ')
    b1 = simp.sympify(b1)

    global c1
    c1 = input('Please specify the value of "c" for the first equation: ')
    c1 = simp.sympify(c1)

def a_inp_eq2():
    global a2
    a2 = input('Please specify the value of "a" for the second equation: ')
    a2 = simp.sympify(a2)

    global b2
    b2 = input('Please specify the value of "b" for the second equation: ')
    b2 = simp.sympify(b2)

    global c2
    c2 = input('Please specify the value of "c" for the second equation: ')
    c2 = simp.sympify(c2)

def check_errors_eq1():
    try:
        int(a1)
        int(b1)
        int(c1)
    except ValueError:
        try:
            float(a1)
            float(b1)
            float(c1)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq1()

def check_errors_eq2():
    try:
        int(a2)
        int(b2)
        int(c2)
    except ValueError:
        try:
            float(a2)
            float(b2)
            float(c2)
        except ValueError:
            print('One of your inputs may not be a valid one please try again, this time with valid ones')
            a_inp_eq2()



def conversion():
    global a_con1
    a_con1 = a1 * -1
    global c_con1
    c_con1 = c1 * -1

    global a_con2
    a_con2 = a2 * -1
    global c_con2
    c_con2 = c2 * -1


# equation solver
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^{x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)


# defining the main plotting function
def Linear_eq(a,b,clr):
    x = list(range(0,11))
    y = [(a * ints + b) for ints in x]
    print(y)
    plt.plot(x , y ,label = 'linear' ,linestyle = '-' ,color= clr)

def plot():
    solx , soly = solve_eq()
    Linear_eq(a_con1,c_con2,'r')
    Linear_eq(a_con2,c_con2,'b')
    plt.annotate('(%s,%s)'%(solx,soly) , (float(solx),float(soly)))
    plt.show()

def run():
    a_inp_eq1()
    a_inp_eq2()
    check_errors_eq1()
    check_errors_eq2()
    conversion()
    plot()

run()
def solve_eq():
    x = simp.Symbol('x')
    y = simp.Symbol('y')
    eq = (a1 * x + b1 * y + c1 , a2 * x + b2 * y + c2)
    global sol
    sol = simp.solve(eq , x , y)
    print(sol)
    m = re.match(r'^ {x: (\d+), y: (\d+)}' , str(sol))
    if m:
        return m.group(1) , m.group(2)

任何帮助都将不胜感激,谢谢您的时间

如果没有匹配项,则返回nothing(None)如何确保其匹配?您可以使用此
r{x:([0-9]\d*(\.\d+))和y:([0-9]\d*(\.\d+)))}”
但为什么不在字符串中查找任何数字
r”([0-9]\d*(\.\d+)”
?我不确定正则表达式如何工作抱歉,我向我的好友寻求帮助,我会尝试r{x:(?)?[0-9]\d*(\.\d+),y:((?[0-9]\d*(\.\d+)))。谢谢!谢谢!谢谢!虽然我遇到了一个新错误,但我会在新帖子中询问