This is a problem that's always vexed me. I've re-made this program multiple times over the years, but the results are the same; the program ignores all the if statements. Its as if it knows the program I'm trying to make and refuses to execute.
Using print commands, the location is being set accurately. There's no reason why it should be ignoring it. Here is my latest attempt to make this program:
topLeft='topLeft'
top='top'
topRight='topRight'
midLeft='midLeft'
mid='mid'
midRight='midRight'
botLeft='botLeft'
bot='bot'
botRight='botRight'
location=mid
def printError():
print("You can't go that way!")
def topLeft():
userInput=input('You can go south or east.')
if userInput=='s':
location=midLeft
elif userInput=='e':
location=top
else:
printError()
def top():
userInput=input('You can go south, east, or west.')
if userInput=='s':
location=mid
elif userInput=='e':
location=topRight
elif userInput=='w':
location=topLeft
else:
printError()
def topRight():
userInput=input('You can go south or west.')
if userInput=='s':
location=midRight
elif userInput=='w':
location=top
else:
printError()
def midLeft():
userInput=input('You can go north, south or east.')
if userInput=='n':
location=topLeft
elif userInput=='s':
location=botLeft
elif userInput=='e':
location=mid
else:
printError()
def mid():
userInput=input('You can go north, south, east, and west.')
if userInput=='n':
location=top
elif userInput=='s':
location=bot
elif userInput=='e':
location=midRight
elif userInput=='w':
location=midLeft
else:
printError()
def midRight():
userInput=input('You can go north, south, west.')
if userInput=='n':
location=topRight
elif userInput=='s':
location=botRight
elif userInput=='w':
location=mid
else:
printError()
def botLeft():
userInput=input('You can go north or east.')
if userInput=='n':
location=midLeft
elif userInput=='e':
location=bot
else:
printError()
def bot():
userInput=input('You can go north, east, or west.')
if userInput=='n':
location=mid
elif userInput=='e':
location=botLeft
elif userInput=='w':
location=botRight
else:
printError()
def botRight():
userInput=input('You can go north or west.')
if userInput=='n':
location=midRight
elif userInput=='w':
location=bot
else:
printError()
while True:
print(location)
if location==topLeft:
topLeft()
elif location==top:
top()
elif location==topRight:
topRight()
elif location==midLeft:
midLeft()
elif location==mid:
mid()
elif location==midRight:
midRight()
elif location==botLeft:
botLeft()
elif location==bot:
bot()
elif location==botRight:
botRight()
Hopefully the code is pretty self-explanatory. Why do such programs keep refusing to work? No other such loops ignore if statements. It does do this under other circumstances too, but this is the only one that has a pattern. I try to make a text-based rpg, it fails right off the bat no matter what I do. Why does it do this? Do while loops simply not like large numbers of if statements? It even does this even if the options are only two though. Am I triggering some sort of failsafe? What's going on here?
You're defining each of your locations (top, mid, and so on) as both a string and a function. The function definition comes later, so it takes precedence. That means every condition in your while
loop is going to fail, as they're all trying to compare strings to functions.
You'll have to rename either the functions or the strings. I'd personally go with the functions since you can find-and-replace for "top()" without messing too much up, but it's up to you.