0

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. 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.

# 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()

1 Answers1

0

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. So if I were to type "2*x+1", you'd end up with y = "2*x+1". Make sure your program can either handle the string, or take whatever information you need out of the string.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53