2014-08-29 2 views
-1

요소 사전에서 작업 중이며 입력을 통해 외부 함수를 실행하는 데 문제가 발생했습니다. 입력을 받아 여러 변수가있는 함수를 호출해야합니다. 외부 함수가 필요한 곳 ​​Python 3.4의 외부 함수

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)이며, 기능이 저장되는 곳 '원자'입니다 : 여기 내 코드입니다.

+1

"외부 기능"이란 무엇입니까? – abarnert

+0

전체 코드를 입력하십시오. –

+0

가져온 모듈의 이름을 사용하는 방법을 묻는 중입니까? 'import os'를 사용하면'os.listdir()'을 호출 할 수 있습니다. 그게 니가 찾고 있던거야? – abarnert

답변

0

나는 사용자

이 당신을 위해 무엇을 찾고 있다면 난 정말 모르겠어요으로 입력 할 수있는 기능이 필요합니다. 하지만 여러분은 파이썬이 First-class function이라는 것을 알아야합니다. 즉, 다른 값과 같은 변수에 함수를 저장할 수 있습니다. varaible 또는 사전에 있음. 그 생각에 익숙해

이 예 실험 시간이 좀 걸릴 : 다른 모든 기능처럼 your_fct를 호출하는 동안 물론

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() 

, 당신은 인수를 전달할 수 있습니다.