-2

Is it possible to store a value obtained from a definition into a variable?

def desks_for_group(x):
    students_per_desk = 2

    if x % 2 == 0:
        x = x
    else:
        x += 1

    desks = x / students_per_desk
    print(desks)

group_1 = int(input())
group_2 = int(input())
group_3 = int(input())

total_desks_required = (desks_for_group(group_1) + desks_for_group(group_2) + 
desks_for_group(group_3))

print(total_desks_required)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

Use return.

After all the computation is done return sends the value back to the function call. Read more on return here.

If you want to store that value as a variable assign the function call to the variable, like this:

var=my_function(arg1,arg1)

This will store the computed value into var.

Here, you can store two or more returned values using:

var1, var2 = my_function(arg1,arg2)

In your case this works like:

def desks_for_group(x):
    students_per_desk = 2

    if x % 2 == 0:
        x = x
    else:
        x += 1

    desks = x / students_per_desk
    return desks

group_1 = int(input())
group_2 = int(input())
group_3 = int(input())

total_desks_required = (desks_for_group(group_1) + desks_for_group(group_2) + 
desks_for_group(group_3))

print(total_desks_required)
Kaustubh Lohani
  • 635
  • 5
  • 15