Home > Enterprise >  how to bring out a variable outside of indention?
how to bring out a variable outside of indention?

Time:07-04

I created a program that has indention in it (you know , like for and while loops , if , elif , else , def and any another thing that has indention after it.) and this indention has some new created variables in it. like this :

          .
          .
          .
little_input = input()
big_input = input()
num = 10
def calculate() :
    global num
    num  = 1
    num2 = num * big_input
    num3 = num2 / little_input
    print(num3)
    rmin = num2 % little_input
    if rmin == 0 :
        print(num3)
           .
           .
           .

and then I saw I need to do something with the num3 variable but it needs to be outside of the def indention. and when it's outside , it says the variable is not defined. can you help me with this? thanks.

edit for @Nesi :

print("chain wheel calculator")
print("enter the little diameter in mm :")
little_input = input()
print("enter the big diameter in mm :")
big_input = input()

little_input = int(little_input)
big_input = int(big_input)

num = 9

def calculate() :
    global num
    num  = 1
    num2 = num * big_input
    num3 = num2 / little_input
    print(num3)
    rmin = num2 % little_input
    if rmin == 0 :
        print(num3)

while num <= 100 :
    calculate()

CodePudding user response:

Try this.

def calculate(num) :
    num2 = num * big_input
    num3 = num2 / little_input
    print(num3)
    rmin = num2 % little_input
    if rmin == 0 :
        print(num3)
    # after you do what you need with this function
    # you will endup with value for num3, right?
    # we will give it to the loop.
    return num3

print("chain wheel calculator")
print("enter the little diameter in mm :")
little_input = input()
print("enter the big diameter in mm :")
big_input = input()

little_input = int(little_input)
big_input = int(big_input)


num = 9
while num <= 100 :
    num3 = calculate(num)  # pass the num to function
    print(num3)
    num  = 1

CodePudding user response:

print("chain wheel calculator")                                                                                                                                                     
print("enter the little diameter in mm :")                                      
little_input = input()                                                          
print("enter the big diameter in mm :")                                         
big_input = input()                                                             
                                                                                
little_input = int(little_input)                                                
big_input = int(big_input)                                                      
                                                                                
num = 9                                                                         
                                                                                
def calculate(little_input, big_input, num):                                    
    num2 = num * big_input                                                      
    num3 = num2 / little_input                                                  
    rmin = num2 % little_input                                                  
    if rmin == 0:                                                               
        return num3                                                             
                                                                                
while num <= 100 :                                                              
    num3 = calculate(little_input, big_input, num)                              
    if num3:                                                                    
        print(num3)                                                             
    num  = 1     

this should work. I am not sure what behaviour you want but I presume you only want to return num3 if rmin == 0. If rmin != 0 this will return None. So the statement if num3: checks if num3 is not None type and then prints the resulting value. You can add in whatever you wish to do to num3 within this conditional statement. Generally, aviod mixing scopes like you were doing before.

CodePudding user response:

You will need to declare it outside the function then inside the function use it as global variable.

Here a link that might help you.

Here an example

num3= 0


def calculate():
        global num3
        num3 = 133131





print(f"Num3 before calling func : {num3}")
calculate()
print(f"Num3 after calling func : {num3}")

Output that i got

Num3 before calling func : 0
Num3 after calling func : 133131
  • Related