简体   繁体   English

在绘制线性方程时,如何让我的程序询问用户输入

[英]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.

相关问题 我如何让我的程序向用户提出更多问题? - How do i make my program ask further questions to the user? 如何使用带有海龟图形的Python制作线性图形计算器? - How do I make a linear graphing calculator in Python with turtle graphics? 如何要求用户输入使代码交互 - How to ask user's input to make a code interactive 发生异常时如何要求用户输入? - How to ask user for input when an exception has occurred? 当用户未登录时,如何让应用程序要求登录。在 KivyMD 中 - How to make the app ask for login when a user is not logged in. In KivyMD 如何要求用户输入数字,然后使程序从2开始加到输入? - How to ask the user to input a number then get the program to add up to the input starting at 2? 如何使用用户输入使程序结束? - How to make a program end using user input? python 程序要求用户输入 n 个数字并告诉他这些数字中最大的一个,当用户键入 0 程序应该停止 - python program ask user to input n number and tell him the biggest among these numbers when the user type 0 the program should stop 为什么在求解非线性方程组时忽略我的一些方程 - Why are some of my equations ignored when solving a non-linear system of equations 如何让一个 python 文件要求用户输入,然后将此输入传递给第二个 .py 文件? - How can I make one python file ask for user input and then pass this input to a second .py file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM