2013-11-01 2 views
0

"전역 이름 'user_input'이 정의되지 않았습니다. 파이썬에 익숙하지 않으므로 도움을 받으실 수 있습니다. 여기 내 코드가 있습니다. . 단지 ask_user()user_input = ask_user()에 말했다 라인을 변경하여 menu 기능에python : 전역 이름 'user_input'이 정의되지 않았습니다.

def menu(): 
    '''list of options of unit types to have converted for the user 
    ex: 
    >>> _1)Length 
     _2)Tempurature 
     _3)Volume 
    ''' 

    print('_1)Length\n' '_2)Temperature\n' '_3)Volume\n' '_4)Mass\n' '_5)Area\n' 
      '_6)Time\n' '_7)Speed\n' '_8)Digital Storage\n') 

    ask_user() 
    sub_menu(user_input) 

def ask_user(): 
    ''' asks the user what units they would like converted 
    ex: 
    >>> what units do you need to convert? meter, feet 
    >>> 3.281 
    ''' 
    user_input = input("Make a selection: ") 
    print ("you entered", user_input) 
    #conversion(user_input) 
    return user_input 

def convert_meters_to_feet(num): 
    '''converts a user determined ammount of meters into feet 
    ex: 
    >>> convert_meters_to_feet(50) 
    >>> 164.042 
    ''' 

    num_feet = num * 3.28084 
    print(num_feet) 


def convert_fahrenheit_to_celsius(num): 
    '''converts a user determined temperature in fahrenheit to celsius 
    ex: 
    >>> convert_fahrenheit_to_celsius(60) 
    >>> 15.6 
    >>> convert_fahrenheit_to_celsius(32) 
    >>> 0 
    ''' 

    degree_celsius = (num - 32) * (5/9) 
    print(round(degree_celsius, 2)) 


def sub_menu(num): 
    '''routes the user from the main menu to a sub menu based on 
    their first selection''' 

    if user_input == '1': 
     print('_1)Kilometers\n' '_2)Meters\n' '_3)Centimeters\n' '_4)Millimeters\n' 
       '_5)Mile\n' '_6)Yard\n' '_7)Foot\n' '_8)Inch\n' '_9)Nautical Mile\n') 

     ask = input('Make a selection (starting unit)') 
     return 
    if user_input == '2': 
     print('_1)Fahrenheit\n' '_2)Celsius\n' '_3)Kelvin\n') 
     ask = input('Make a selection (starting unit)') 
     return 
+1

'user_input'은'ask_user()'의 범위에 정의되어 있고'sub_menu()'안에서 검사합니다. – zero323

답변

0

을 당신이 할 경우 :

user_input = input("Make a selection: ") 

ask_user() 함수 내부에서 해당 함수 안에 user_input에만 액세스 할 수 있습니다. 이 변수는 해당 범위에만 포함 된 로컬 변수입니다. 당신이 다른 곳에서 액세스하려면

, 당신은 그것을 세계화 할 수 있습니다

global user_input 
user_input = input("Make a selection: ") 

난 당신이 출력을 반환 한 후 사용하는 것이 었습니다하려고했던 것 같아요. 당신은 그것을 가지고 있지만, 대신 ask_user(), 당신은 변수에 반환 된 데이터를 넣어야 해. 따라서 :

이 방법을 사용하면 변수를 전역화할 필요가 없습니다 (앞에서 설명한대로).

+0

감사합니다! 어젯밤에 그게 날 미치게 했어. 그것은 그것이 작동하는 것을 보았을 때 많은 의미가 있습니다. – borgy88

0

... 자신을 시작하고 가르치는

관련 문제