调用另一个函数内部的变量

时间:2020-05-31 14:41:38

标签: python

def HOME():
    """ The first screen
    """

    print ('Welcome to my program!')
    input_grocery_list = input('Hello, please enter here a list of groceries: ')

    def input_to_list():
        """ This f takes the input 
        and put it inside a list 
        without the: ','
        and print the list
        """
        new_grocery_list = []
        separator = ", "
        while ',' in input_grocery_list:
            d = input_grocery_list.index(',') 
            y = input_grocery_list[:d]
            new_grocery_list.append(y)
            input_grocery_list = input_grocery_list[(d+1):]
            if ',' not in input_grocery_list:
                new_grocery_list.append(input_grocery_list)
        print(separator.join(new_grocery_list))


    def the_groceries_list():
        """ Do somthing  to the first input
        accord to the next choice
        """

        print("You can chose a number betwen 1 - 9 n/ and we do the rest..")
        grocery_list = input('please enter here your choice : ') 
        if grocery_list == '1':
            input_to_list(input_grocery_list)

    if len(input_grocery_list) != 0:
        the_groceries_list()
HOME()

错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 37, in HOME
  File "<stdin>", line 34, in the_groceries_list
TypeError: input_to_list() takes 0 positional arguments but 1 was given

我的问题可能出在第二个函数“ input_to_list()

当我尝试读取/更改第一个函数( HOME )中的“ input_grocery_list ”变量时 它不认识他。

我尝试使用 global self 修复它,但我可能真的不知道确切的位置放置它们,因为与此同时它对我不起作用。

有人知道我该如何解决吗?

2 个答案:

答案 0 :(得分:0)

正如我所看到的,您尝试拥有一个主函数,但是应该使用类而不是函数,所以更像这样。

class Home():
    def __init__(self):
        """
        define your class wide variables here as following
        """
        # example
        # self.your_name = 'hello'
        # to access it in an function IN this class, type: self.your_name
        pass

    def get_items(self):
        print('Welcome to my program!')
        input_grocery_list = input('Hello, please enter here a list of groceries (sep with comma): ')
        self.input_to_list(input_grocery_list)

    #                       you forgot this
    #                               ||
    #                               \/
    def input_to_list(self, grocery_list: str):
        """ This f takes the input
        and put it inside a list
        without the: ','
        and print the list
        """
        new_grocery_list = grocery_list.split(',')
        # to print as list
        print(new_grocery_list)
        # to print every item in list
        for item in new_grocery_list:
            print(item)
        # print('List End') # if you want to

    # I don't really know what that is supposed to do, but whatever it is it wont
    """
    def the_groceries_list(self):
        # Do somthing  to the first input
        # accord to the next choice

        print("You can chose a number betwen 1 - 9 n/ and we do the rest..")
        grocery_list = input('please enter here your choice : ')
        if grocery_list == '1':
            input_to_list(input_grocery_list)

        if len(input_grocery_list) != 0:
            the_groceries_list()
    """


# to use this class and it's functions first create an object
home = Home()

# than call a function
home.get_items()

我不会详细讨论类和对象but here is a link for you。 阅读此书,它应该能很好地说明基本知识。

答案 1 :(得分:0)

您需要更改嵌套函数的定义,以便可以使用args:

def input_to_list(input_grocery_list):