PY How to remove variables from function
-
Started studying the python, tell me what I'm doing wrong? I don't have any value from my function, and the conclusion to the Visual Studio console looks like this: [Running] python -u "c:\Users\SNR93\Desktop\test.py." Traceback (most recent call last): File, line 17, in print(", cube_number, ", cube_edge," NameError: name 'cube_line_edge' is not defined
[Done] exited with code=1 in 0.05 seconds
# глобальные переменные cube_number = 2 #указываем количество кубов cube_edge = 3 #указываем длину ребра в метрах
#делаем функцию
def cube_func(cube_number, cube_edge):
global cube_m_2, cube_line_edge
# считаем, сколько нам понадобится линий для кубов
cube_lines = cube_number * 12
# переводим палки в метры
cube_line_edge = cube_lines * cube_edge
# S = a^2 * 6
cube_m_2 = (cube_edge**2 * 6) * cube_number
return (cube_m_2, cube_line_edge)print("У вас", cube_number, "куб с длиной ребра", cube_edge,"м. Вам понадобится", cube_line_edge, " метров палок и", cube_m_2, "м^2 стекла")
-
You didn't call a function.
Note that the parameters in the functions may have names other than the names of variables. Only their location is relevant.
par_1 = 17 par_2 = 24
def func(par_1, par_2):
print(par_1, par_2)func(par_2, par_1) #Выведет сначала 24, а потом 17
There are also position parameters in the functions that are default and where the function is demanded by 3 values, and when called by only 2, the positioning argument will be default.
Important: positioning arguments are always the last of the functions.
par_1 = 17
def func(par_1, par_2 = 7): #У par_2 значение по умолчанию = 7
print(par_1, par_2)func(par_1) #Выведет сначала 17, а потом 7
The solution to your problem is:
# глобальные переменные
cube_number = 2 #указываем количество кубов
cube_edge = 3 #указываем длину ребра в метрах#делаем функцию
def cube_func(cube_number, cube_edge):
global cube_m_2, cube_line_edge
# считаем, сколько нам понадобится линий для кубов
cube_lines = cube_number * 12
# переводим палки в метры
cube_line_edge = cube_lines * cube_edge
# S = a^2 * 6
cube_m_2 = (cube_edge**2 * 6) * cube_number
return (cube_m_2, cube_line_edge)
print(cube_func(cube_number,cube_edge))