[英]How to make my program ask for user input when graphing linear equations
I am making an algebra calculator for my CSSE (Computer Science and Software Engineering) class and I am having trouble with a portion of my project. 我正在为我的CSSE(计算机科学和软件工程)课程编写一个代数计算器,我在项目的一部分上遇到了麻烦。 I am trying to have my program ask the user for some input to so it can graph the given equation.
我试图让我的程序向用户询问一些输入,以便它可以绘制给定的等式。 I do have it set up to where it graphs an already set equation.
我确实将它设置为绘制已经设定的等式的位置。
I have already tried making the "y = " variable ask for user input but nothing seems to be working. 我已经尝试使用“y =”变量询问用户输入但似乎没有任何工作。
# Juan Salcedo
# Plotting linear graphs
# April 26, 2019
# Calling necessary libraries
import matplotlib.pyplot as plt
import numpy as np
# Defining x and y
x = np.linspace(-5, 5, 100)
y = 2*x+1
# Calling variables x and y
# Colouring graph "red" and labeling graph
plt.plot(x, y, '-b', label='y=2x+1')
# Titling plot
plt.title('Graph of y=2x+1')
# Colouring graph in hex
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
# Giving legend
plt.legend(loc='upper left')
plt.grid()
# Calling/showing plot
plt.show()
If you just want to get input from the command line, do this: 如果您只想从命令行获取输入,请执行以下操作:
y = input("Enter your equation: ")
The built-in function input()
will pause the program until the user types something and presses Enter, and then returns whatever they typed as a string. 内置函数
input()
将暂停程序,直到用户键入内容并按Enter键,然后返回键入的任何内容作为字符串。 So if I were to type "2*x+1", you'd end up with y = "2*x+1"
. 所以如果我输入“2 * x + 1”,你最终会得到
y = "2*x+1"
。 Make sure your program can either handle the string, or take whatever information you need out of the string. 确保您的程序可以处理字符串,或者从字符串中获取所需的任何信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.