2016-11-12 1 views
0

텍스트 기반 게임을 만들려고하고 있지만 한 함수에서 다른 함수로 일부 변수를 전달하는 데 문제가 있습니다. 함수 내에서 변수를 수정하고 새 값을 반환하여 원본을 덮어 쓰는 방법을 알아 냈습니다. 나는 if 문을 잠금을 해제 something1(x)something2(y) 다시 room1()room2() 변수를 얻는 방법입니다 도움과 main()에 필요한 것은하나의 함수에서 다른 함수로 로컬 변수를 전달하는 방법은 무엇입니까?

.

something1(x)something2(y)에 두 가지 기능 또는 하나의 기능을 사용해야합니까?

이 나는 ​​데 문제가있는 일반 샘플 코드입니다 :

def something1(x): 
    x += 0 
    return x 

def something2(y): 
    y += 0 
    return y  

def main(): 
    print("1. Try to open door") 
    print("2. Go to room1") 
    print("3. Go to room2") 
    choice = int(input("Enter selection: ") 
    if choice == "1": 

    # Trying to get this if statement to work with the variables 
    # Don't know which function or parameters to pass in order to get it to work 

     if x == 3 and y == 2: 
      print("You're free") 
     else: 
      print("You're not free") 
    elif choice == "2": 
     room1() 
    elif choice == "3": 
     room2() 
    else: 
     print("ERROR") 
     main() 

def room1(): 
    print("1. Push thing1") 
    print("2. Push thing2") 
    print("3. Push thing3") 
    print("4. Return to previous room") 
    pushChoice = input("Enter selection: ") 
    if pushChoice == "1": 
     print("Thing1 pushed") 
     room1() 
    elif pushChoice == "2": 
     print("Thing2 pushed") 
     room1() 
    elif pushChoice == "3": 
     print("Thing3 pushed") 

    # The modified variable x for something1(x) 

     x = 3 
     x = something1(x) 
     room1() 
    elif pushChoice == "4": 
     main1() 
    else: 
     print("ERROR") 
     room1() 

def room2(): 
    print("1. Pull thingA") 
    print("2. Pull thingB") 
    print("3. Pull thingC") 
    print("4. Return to previous room") 
    pullChoice = input("Enter selection: ") 
    if pullChoice == "1": 
     print("ThingA pushed") 
     room1() 
    elif pullChoice == "2": 
     print("ThingB pushed") 

     # The modified variable y for something2(y) 

     y = 2 
     y = something1(y)  
     room1() 
    elif pullChoice == "3": 
     print("ThingC pushed") 
     room1() 
    elif pullChoice == "4": 
     main1() 
    else: 
     print("ERROR") 
     room1() 

답변

0

당신 pass 수 변수를 반환하여 다른 하나의 함수에서 변수. 그러나 그렇게하기 위해서는, 함수는 예를 들어, 함수 본문 내에서 다른 함수를 호출 할 수 있습니다

def addandsquare(x, y): 
    y = squarefunction(x+y) # sum x+y is passed to squarefunction, it returns the square and stores it in y. 
    return y 

def squarefunction(a): 
    return (a*a) # returns the square of a given number 

print(addandsquare(2, 3)) # prints 25 

을 그러나 그것의 신체 내에서 함수를 호출 할 수 있지만, 로컬을 사용하고자하는 경우 그 함수의 변수를 선언하면 그 변수를 두 함수 모두에 선언 할 수 있습니다. 이 도움이

globvar = 0 

def set_globvar_to_one(): 
    global globvar # Needed to modify global copy of globvar 
    globvar = 1 

def print_globvar(): 
    print globvar  # No need for global declaration to read value of globvar 

set_globvar_to_one() 
print_globvar()  # Prints 1 

희망 :

다음은 그 예입니다!

+0

안녕하세요 @ 답변이 답변이 문제를 해결 한 경우 체크 표시를 클릭하여 [수락] (http://meta.stackexchange.com/q/5234/179419)하십시오. 이는 해결책을 찾았고 응답자와 자신에게 어느 정도의 평판을 제공한다는 것을 더 넓은 커뮤니티에 나타냅니다. 이를 수행 할 의무는 없습니다. –

관련 문제