Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/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 3.4中的外部函数_Python_Windows_Python 3.x - Fatal编程技术网

Python 3.4中的外部函数

Python 3.4中的外部函数,python,windows,python-3.x,Python,Windows,Python 3.x,我一直在编写一个元素字典,在通过输入运行外部函数时遇到了一个问题。我需要获取一个输入并使用它调用一个包含多个变量的函数。这是我的密码: import atoms import time print("Hello, and welcome to the element dictionary. This app takes an element symbol") print(" and outputs a small amount of data about the element.") eleme

我一直在编写一个元素字典,在通过输入运行外部函数时遇到了一个问题。我需要获取一个输入并使用它调用一个包含多个变量的函数。这是我的密码:

import atoms
import time
print("Hello, and welcome to the element dictionary. This app takes an element symbol")
print(" and outputs a small amount of data about the element.")
element=input("please input an elements symbol :")
(element)
print('catagory: ',cat )
print(' atomic number: ',atomn)
print(' atomic weight: ',atomw)
print('colour: ',colour )
print(' phase: ',phase )
print(' melting point: ',meltpoint)
print('boiling point: ',boilpoint)
print('crystal structure: ',cstruc)
time.sleep(100)
(element)
是需要外部函数的地方,“atoms”是存储函数的地方

我需要由用户输入的功能

我真的不知道这是否是你想要的。但是你必须知道Python有所谓的。也就是说,您可以像任何其他值一样将函数存储在变量中。在字典或字典里

要熟悉该想法,请花些时间尝试以下示例:

def f():
    print("This is f")

def g():
    print("This is g")

def other():
    print("Other choice")

actions = {
   "f": f,
   "F": f,
   "g": g,
   "G": g
}

your_choice=raw_input("Choose f or g: ")
your_fct = actions.get(your_choice, other)
#                                   ^^^^^
#                                default value

your_fct()

当然,您可以在调用
您的\u fct
时传递参数,就像任何其他函数一样。

您所说的“外部函数”是什么意思?请提供您的全部代码。您只是问如何使用导入的模块的名称吗?如果
导入os
,则可以调用
os.listdir()
。这就是你要找的吗?@abarnert,我想从这个项目专用文件夹中的一个文件导入。那么…是吗?如果没有,你需要更好地解释。如果是这样,您的代码表明您已经知道如何从
时间
调用函数;是什么让您认为从原子调用函数会有所不同?