DavidK DavidK - 2 months ago 4
Python Question

How can I print out the correct response based on input?

I'm new to Python and I have a very simple problem that I'd like explained to me. I'm making a text adventure game and I'd like to be able to call another function inside a function so my code is organized better.

This is what I have that's working:

def displayIntro():
print("You come to a crossroads on your trip home.")
print ("You can go LEFT towards the DEMON FOREST or RIGHT to SKULL MOUNTAIN")
print()

def choosePath():
path = ""
while path != "LEFT" and path != "RIGHT":
path = input("Which path will you choose? LEFT or RIGHT? --> ")
if path == "RIGHT":
print("You take the path to SKULL MOUNTAIN.")
elif path == "LEFT":
print ("You take the path to the DEMON FOREST.")




displayIntro()
choosePath()
choosenPath()


But I'd like it to look something like this, but I don't know how to make it work:

def displayIntro():
print("You come to a crossroads on your trip home.")
print ("You can go LEFT towards the DEMON FOREST or RIGHT to SKULL MOUNTAIN")
print()

def choosePath():
path = ""
while path != "LEFT" and path != "RIGHT":
path = input("Which path will you choose? LEFT or RIGHT? --> ")
return path


def checkPath(choosePath):

if choosePath == "RIGHT":
print("You take the path to SKULL MOUNTAIN.")
elif choosePath == "LEFT":
print("You take the path to the DEMON FOREST.")



displayIntro()
choosePath()
checkPath()

Answer

You have to pass the return value of choosePath to checkPath.

checkPath(choosePath())

checkPath takes a parameter, so you need to supply a value. That value is the return value of choosePath, either "LEFT" or "RIGHT". Once you pass it in, the checkPath function will execute, checking the string and printing accordingly. Replace the choosePath and checkPath call with the above code.