簡體   English   中英

在繪制線性方程時,如何讓我的程序詢問用戶輸入

[英]How to make my program ask for user input when graphing linear equations

我正在為我的CSSE(計算機科學和軟件工程)課程編寫一個代數計算器,我在項目的一部分上遇到了麻煩。 我試圖讓我的程序向用戶詢問一些輸入,以便它可以繪制給定的等式。 我確實將它設置為繪制已經設定的等式的位置。

我已經嘗試使用“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()

如果您只想從命令行獲取輸入,請執行以下操作:

y = input("Enter your equation: ")

內置函數input()將暫停程序,直到用戶鍵入內容並按Enter鍵,然后返回鍵入的任何內容作為字符串。 所以如果我輸入“2 * x + 1”,你最終會得到y = "2*x+1" 確保您的程序可以處理字符串,或者從字符串中獲取所需的任何信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM