在Python中,输出横线可以通过以下几种方式实现:
1. 使用`print`函数和字符串乘法:
def print_hline(length=30):print('-' * length)print_hline()
2. 根据用户输入的数字打印相应数量的横线:
def print_hline_by_number():x = int(input("请输入打印次数:"))for _ in range(x):print('-' * 30)print_hline_by_number()
3. 使用`matplotlib.pyplot`模块绘制横线:
import matplotlib.pyplot as pltdef plot_hline(y_value, xmin, xmax, color='red'):plt.hlines(y_value, xmin, xmax, color=color)plt.figure(figsize=(8, 6))绘制一些数据x = np.arange(0.0, 12 * np.pi, 0.01)y = np.sin(t) * (np.e np.cos(t) - 2 * np.cos(4 * t) - np.sin(t / 12) 5)plt.plot(x, y, color='blue', linewidth=3)添加横线plot_hline(0, -3, 3, color='red')plt.show()

