Python 调用函数的用户输入始终以进程0结束
我正在尝试制作一个程序,根据用户希望看到的内容显示exp或log函数。因此,如果用户想要体验乐趣,那么他会写1。如果日志有趣,那么2 问题是,在输入程序后,程序总是以“进程结束,退出代码为0”结束,并且没有显示任何功能 解决办法是什么 代码:Python 调用函数的用户输入始终以进程0结束,python,numpy,matplotlib,Python,Numpy,Matplotlib,我正在尝试制作一个程序,根据用户希望看到的内容显示exp或log函数。因此,如果用户想要体验乐趣,那么他会写1。如果日志有趣,那么2 问题是,在输入程序后,程序总是以“进程结束,退出代码为0”结束,并且没有显示任何功能 解决办法是什么 代码: import matplotlib.pyplot as plt import numpy as np class the_process(object): def logfun(self): a = 2 q = 0 x = n
import matplotlib.pyplot as plt
import numpy as np
class the_process(object):
def logfun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.log(a ** (np.add(x, q)))
plt.figure()
plt.plot(x, y)
plt.ylabel('$\log(x)$')
plt.grid(True)
plt.show()
def expfun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.add(np.power(a, x), q)
plt.figure()
plt.plot(x, y)
plt.ylabel('$\exp(x)$')
plt.grid(True)
plt.show()
def __init__(self):
self.a = input(
"Which function do you want to use? \n\n 1) The first one. \n\n 2) The second one. \n\n Please enter the corresponding number and hit enter >>>>> ")
if self.a == 1:
self.expfun()
elif self.a == 2:
self.logfun()
a = the_process();
示例中的缩进是错误的
expfun()
和\uuuu init\uuuu()
不缩进。另外,您应该将其强制转换为int,以便在if
import matplotlib.pyplot as plt
import numpy as np
class the_process(object):
def logfun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.log(a ** (np.add(x, q)))
plt.figure()
plt.plot(x, y)
plt.ylabel('$\log(x)$')
plt.grid(True)
plt.show()
def expfun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.add(np.power(a, x), q)
plt.figure()
plt.plot(x, y)
plt.ylabel('$\exp(x)$')
plt.grid(True)
plt.show()
def __init__(self):
self.a = int(input(
"Which function do you want to use? \n\n 1) The first one. \n\n 2) The second one. \n\n Please enter the corresponding number and hit enter >>>>> "))
if self.a == 1:
self.expfun()
elif self.a == 2:
self.logfun()
a = the_process()
最后,在我看来,你的问题不应该被构造成一个类,所以你可以考虑这一个:
import matplotlib.pyplot as plt
import numpy as np
class TheProcess(object):
def log_fun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.log(a ** (np.add(x, q)))
plt.figure()
plt.plot(x, y)
plt.ylabel("$\log(x)$")
plt.grid(True)
plt.show()
def exp_fun(self):
a = 2
q = 0
x = np.linspace(-2, 4, 100)
y = np.add(np.power(a, x), q)
plt.figure()
plt.plot(x, y)
plt.ylabel("$\exp(x)$")
plt.grid(True)
plt.show()
chosen_option = int(
input(
"Which function do you want to use? \n\n 1) The first one. \n\n 2) The second one. \n\n Please enter the corresponding number and hit enter >>>>> "
)
)
my_process = TheProcess()
if chosen_option == 1:
my_process.exp_fun()
elif chosen_option == 2:
my_process.log_fun()
else:
print("Exiting...")