在python中从用户输入绘制图形

在python中从用户输入绘制图形,python,list,if-statement,graph,Python,List,If Statement,Graph,我正在编写一个程序,该程序接受用户输入,要求提供一个图形,然后将该图形打印到屏幕上,轴的限制范围为-10到10。我知道您应该使用这个有限的范围和域,并根据用户输入的图形进行检查,但我正在努力处理y值,并让它在图形上打印坐标 import math y = [] # input the function from the user func = input ("Enter a function f(x):\n") print () for row in range (10, -11, -1):

我正在编写一个程序,该程序接受用户输入,要求提供一个图形,然后将该图形打印到屏幕上,轴的限制范围为-10到10。我知道您应该使用这个有限的范围和域,并根据用户输入的图形进行检查,但我正在努力处理y值,并让它在图形上打印坐标

import math
y = []

# input the function from the user
func = input ("Enter a function f(x):\n")
print ()

for row in range (10, -11, -1):
    y_value = eval (func.replace ("x", str (row)))
    y.append (y_value)

# where the coord is defined 
for row in range (10, -11, -1):
    for column in range (-10, 11):
        # this is where the program should check for, and output a coordinate
            print ("o", end="")
        if column == 0 and row == 0:
            print ("+", end="")
        elif row == 0:
            print ("-", end="")
        elif column == 0:
            print ("|", end="")
        else:
            print (" ", end="")
    print ()
问题中给出的用户输入是x+2,这意味着输出应该与此图像相同

但我一直得到的是


任何意见都将不胜感激。

您的方法是使用matplotlib绘制图形。首先请注意,y值列表的顺序相反,其索引与用于计算的x值偏移。如果您同时考虑了这两种情况,那么应该能够使用“If y[column]==row”的一些变体。我确实有一个工作版本,但我不想说太多。这更有意义,非常感谢!