Home > Enterprise >  How can I take another variable from a def function and add it to another def function to get a new
How can I take another variable from a def function and add it to another def function to get a new

Time:10-28

def room1(phone_charge):
    phone_charge = 5
    import random
    randNum = random.randint(1,5)
    print("An outlet! You quickly plug in your phone, but the wiring in the house is faulty and soon shorts out.\n")
    positve = str(phone_charge   randNum)
    print("Your phone is now "   positve   " % charged\n")
    return(positve)

I need to add positive to another function

def room5(phone_charge):
    import random
    randomNUM = random.randint(1,30)
    
    positve2= str(phone_charge   randomNUM)
    print("Your phone is now "   positve2   " % charged\n")
    return(positve2)

I need to add postive to the room5 variable postive2

I tried returning variables and putting them in the next function but then my code that was written behind where I entered the returning variable it was no longer highlighted

CodePudding user response:

Since the two functions return their values, you can add them together after calling.

p1 = room1(phone_charge)
p5 = room5(phone_charge)

print(f"Total is {p1   p5}")

CodePudding user response:

Since the two functions have the same functionality use one function with an extra parameter.

import random

def room(phone_charge, rand_range):
    randNum = random.randint(1,rand_range)
    print("An outlet! You quickly plug in your phone, but the wiring in the house is faulty and soon shorts out.\n")
    positve = str(phone_charge   randNum)
    print("Your phone is now "   positve   " % charged\n")
    return positve

room1 = room(5, 5)
room5 = room(10, 30)

total = room1   room5
  • Related