0

I'm trying to find a way to access the confirmpath() function in the pathcond() function. Please help.

PS. I just began learning Python about a week ago, so any help regarding my code brevity and improving my overall skills will be really appreciated.

Following is the code I need help with:

def name():
    global call
    call = raw_input("What is your name?\n")
    print("Hello " + call)

def game():
    global charchoose
    charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n")
    print("You chose " + charchoose)

def path():
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower()
    def confirmpath():
        global confirmpath
        confirmpath = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower()
        pathcond()
    confirmpath()

def pathcond():
    while confirmpath == "no":
        path()
    if confirmpath == "yes":
        print("Good choice, you win!")
    else:
        print("Sorry, we didn't get that. Can you answer again, please?")
        confirmpath()

def ask():
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
    if askplay == "yes":
        game()
        path()
    elif askplay == "no":
        print("That's alright. Thanks for hanging out, though. Bye!")
    else:
        print("Sorry, I didn't get that. Please try again.")
        ask()

name()
ask()

UPDATE: I worked further on the program for sometime and ended up with a version that is now problem free. I've posted it below, please help me make it better by suggesting what I could change/improve/remove. The code is as follows:

def Initiate():
   global call
   call = raw_input("What is your name?\n")
   print("Hello " + call)
   begin()

def game():
   global charchoose
   charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n")
    print("You chose " + charchoose)
    path()

def path():
    global pathchoose
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower()
    confirmpath()

def confirmpath():
    global confirmpaths
    confirmpaths = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower()
    pathcond()

def pathcond():
    while confirmpaths == "no":
        path()
    if confirmpaths == "yes":
        print("Good choice, you win!")
    else:
        print("Sorry, we didn't get that. Can you answer again, please?")
        confirmpath()


def begin():
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
    if askplay == "yes":
        game()
    elif askplay == "no":
        print("That's alright. Thanks for hanging out, though. Bye!")
    else:
        print("Sorry, I didn't get that. Please try again.")
        ask()

Initiate()

UPDATE 2: The code functions properly but still ends up printing the following string numerous times

if confirmpaths == "yes":
    print("Good choice, you win!")

I observed that it prints the string as many times as I reply to the confirmpath() function, regardless of whatever my response is.

2 Answers2

1

You got some issues here, I'll fix them for you.

def name():
    global call
    call = raw_input("What is your name?\n")
    print("Hello " + call)

def game():
    global charchoose
    charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n")
    print("You chose " + charchoose)

def path():
    pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower()
    confirmpath(pathchoose)

def confirmpath(pathchoose):
    confirmpath_var = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower()
    pathcond(confirmpath_var)

def pathcond(confirmpath_var):
    while confirmpath_var == "no":
        path()
    if confirmpath_var == "yes":
        print("Good choice, you win!")
    else:
        print("Sorry, we didn't get that. Can you answer again, please?")
        confirmpath()

def ask():
    askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
    if askplay == "yes":
        game()
        path()
    elif askplay == "no":
        print("That's alright. Thanks for hanging out, though. Bye!")
    else:
        print("Sorry, I didn't get that. Please try again.")
        ask()

name()
ask()

Explanation: First off, just put your functions on the global scope because in this context it doesn't make sense to do what you are doing (defining a function within a function) if you are calling that inner function in different places. Secondly, instead of using global variables, pass variables into functions. Using global variables that functions use is not good practice and leads to spaghetti code and shadow variables Why are global variables evil?. Use the parameters of functions to pass values into it.

I tested the code above, it works.

Community
  • 1
  • 1
Paul Carlton
  • 2,785
  • 2
  • 24
  • 42
  • The code still give me an error whenever I purposely try to create a situation to execute the else condition in the pathcond(confirmpath_var) function. – Arhum Ishtiaq Mar 21 '16 at 18:11
1

In essence, you're trying to create a Finite State Machine. In Python, this is probably best implemented using a class. Here's a similar example that may be useful.

The important part about a class is that it can keep track of the data in your game and allows you to avoid global variables. Here's your program converted to a class.

class Game:
    def __init__(self):
        self.name = raw_input("What is your name?\n")
        print("Hello " + self.name)
        self.ask()

    def ask(self):
        askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
        if askplay == "yes":
            self.game()
        elif askplay == "no":
            print("That's alright. Thanks for hanging out, though. Bye!")
        else:
            print("Sorry, I didn't get that. Please try again.")
            self.ask()

    def game(self):
        self.charchoose = raw_input("What will be your character " + self.name + ": "
            "Mage, Wizard or Knight?\n")
        print("You chose " + self.charchoose)
        self.path()

    def path(self):
        pathchoose = raw_input("You are a " + self.charchoose + " "
            "who was walking down Tranversia and came across a three-way road. "
            "Which on will you choose? Land, Sea or Clouds\n").lower()
        self.confirmpath(pathchoose)

    def confirmpath(self, pathchoose):
        confirmpaths = raw_input("You chose " + pathchoose + ". "
            "Are you sure you want to continue? Yes or No?\n").lower()
        self.pathcond(confirmpaths, pathchoose)

    def pathcond(self, confirmpaths, pathchoose):
        if confirmpaths == "no":
            self.path()
        if confirmpaths == "yes":
            print("Good choice, you win!")
        else:
            print("Sorry, we didn't get that. Can you answer again, please?")
            self.confirmpath(pathchoose)

Game()

When Game() is called, an object (an instance) is created. All of the instance methods in the class refer to self which is the name of this instance.

While this may seem like a bit much, classes can be very useful and are a good concept to get familiar with.

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36