DavidK DavidK - 3 months ago 8
Python Question

How can I print out the correct response based on input

Im new to python and I have a very simple problem that id like explained to me. Im making a text adventure game and Id like to be able to call another function inside a function so my code is organized better.

This is what I have thats 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 Id 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.