Как получить доступ к переменной функции вне функции - PullRequest
0 голосов
/ 21 февраля 2019

Я пытаюсь сложить значения двух функций вместе.Каким образом я могу получить доступ к переменной функции, чтобы она была видна другим функциям.Во фрагменте кода я специально пытаюсь работать с пользовательским вводом переменной product_qty.

я получаю эту ошибку print (Accounting_Menu.product_qty) AttributeError: у объекта 'function' нет атрибута 'product_qty'

def main():

   Accounting_Menu()
   print (Accounting_Menu.product_qty)



def Accounting_Menu():
    print('COMPANY MESSAGE', '\n' *5)
    print('--> Quick Estimates <--')
    product_num = input('> Shoe Model(model number): ')
    product_size = input('> Shoe Size: ')
    product_qty = input('> Quantitiy: ')
    ship_zip_code = input('> Ship to Zip Code: ')
    return product_qty

main()

1 Ответ

0 голосов
/ 21 февраля 2019

Ваша функция может вернуть словарь, содержащий различные вводимые пользователем данные.И использовать его из основного, используя accounting_input ['key_you_want_to_retrieve']

def main():
   accounting_input=Accounting_Menu()
   #print the quantity
   print(accounting_input['product_qty'])
   #print the size
   print(accounting_input['product_size'])

def Accounting_Menu():
    print('COMPANY MESSAGE', '\n' *5)
    print('--> Quick Estimates <--')
    product_num = input('> Shoe Model(model number): ')
    product_size = input('> Shoe Size: ')
    product_qty = input('> Quantitiy: ')
    ship_zip_code = input('> Ship to Zip Code: ')
    input_dictionary = {
        'product_num' : product_num,
        'product_size' : product_size,
        'product_qty' : product_qty,
        'input_dictionary' : ship_zip_code, 
    }
    return input_dictionary

main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...