Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用户打开/关闭功能_Python_Python 3.x_Function_User Interface_Optional - Fatal编程技术网

Python 用户打开/关闭功能

Python 用户打开/关闭功能,python,python-3.x,function,user-interface,optional,Python,Python 3.x,Function,User Interface,Optional,我有一个使用一些函数的代码: def 1(...): return .... def 2(...): return .... def 3(...): return .... 在程序结束时,我们有一个方程,上面的函数都用在其中。我们如何给用户一个类似于下面我展示的选项,来选择应该使用哪个功能 我想要这样的东西 #for using a function write T and for stopping its usage write F Use 1 = F Use 2

我有一个使用一些函数的代码:

def 1(...):
    return ....

def 2(...):
    return ....

def 3(...):
    return ....
在程序结束时,我们有一个方程,上面的函数都用在其中。我们如何给用户一个类似于下面我展示的选项,来选择应该使用哪个功能

我想要这样的东西

#for using a function write T and for stopping its usage write F
Use 1 = F
Use 2 = T
Use 3 = T
因此,函数1关闭,函数2和3打开并在代码中使用


非常感谢。

您可以使用字典并允许用户选择功能

def vash(x):
    return x + 1
def the(x):
    return x * 2
def stampede(x):
    return pow(x, 3)

d = {'vash': vash, 'the': the, 'stampede': stampede}     
x = 2

print('Which functions to turn on: \n\tvash: add 1 \n\tthe: double, \n\tstampede: raise to 3rd power')
on = []
while True:
    choice = input('Enter function to turn on ("exit" when finished): ')
    if choice == 'exit':
        break
    if choice not in d.keys():
        print('Not a valid function.')
        continue
    on.append(choice)

for i in on:
    x = d[i](x)
    print(x)

首先,您需要使用input()从用户处获得每个函数的答案 然后,需要为每个函数使用布尔值,以允许或阻止使用该函数:

我认为您正在寻找smthg,如:

def function1():
   return 2
def function2():
   return 4
def function3():
   return 6  
use_1 = input("Type T to use 1 ")
use_2 = input("Type T to use 2 ")
use_3 = input("Type T to use 3 ")
x=(use_1=='T')*function1()+(use_2=='T')*function2()+(use_3=='T')*function3()
print(x)

你的问题是什么:如何通过打印和输入创建UI,或者如何根据用户的选择创建和处理函数
x
。@MichaelButscher亲爱的Michael,正如我提到的,我想让用户从第一个函数中选择在代码中使用哪一个函数。因为我们有非常复杂的函数,而且函数太多了,我们不能一个接一个地删除它们。是的,但是您在这方面遇到了什么问题?您不知道如何请求用户输入吗?你不知道如何根据收到的信息做出决定吗?您不知道如何“关闭”功能吗?(如果这是最后一个,那么您必须比“关闭”更精确地指定您的需求。)我认为您必须不断澄清。有什么优先权问题吗?方程式中的优先级?我有一个解决方案,让你把所有的函数加在一起。但你在问题中提到了add、sub、mul和div。是的,那些呢?如何决定是否需要添加、sub、mul或div?我在下面的答案海报中给出了一个简单的例子,我希望这对您有所帮助谢谢,让我解释一下。我们有一个10000行的代码。在这段代码中,我们有10多个函数。在整个代码中应该使用这10个函数中的哪一个?我们不知道,这将由用户决定。如果用户打开1,4,6,7,那么我们有一个使用这些函数的代码,而其他函数已经不用了。如果用户变为8,9 10,我们有一个使用函数8 9 10的代码,其他函数都关闭了。@Ehsan我找到你了,一个moment@Ehsan这个怎么样谢谢,我想它会有用的。事实上,并非所有函数都有类似的参数。def1(x,y,z),def2(x,d,f),def3(z,r,t)…但是让我检查一下,我检查一下这篇文章作为答案。谢谢,让我解释一下。我们有一个10000行的代码。在这段代码中,我们有10多个函数。在整个代码中应该使用这10个函数中的哪一个?我们不知道,这将由用户决定。如果用户打开1,4,6,7,那么我们有一个使用这些函数的代码,而其他函数已经不用了。如果用户打开8,9 10,我们有一个代码使用函数8,9 10,其他函数关闭。上面的代码应该可以帮助你做到这一点,我举了一个例子,有3个函数,但你可以扩展到10数量并不重要,因为打开和打开的方式是最重要的,打开或关闭的方式是通过布尔值。在上面的代码中使用(use_1=='T'),如果它是真的,那么我们允许使用,如果不是,我们不允许。是的,请让我检查,我会检查帖子作为答案。非常感谢。
def function1():
   return 2
def function2():
   return 4
def function3():
   return 6  
use_1 = input("Type T to use 1 ")
use_2 = input("Type T to use 2 ")
use_3 = input("Type T to use 3 ")
x=(use_1=='T')*function1()+(use_2=='T')*function2()+(use_3=='T')*function3()
print(x)