Reputation: 25
I have finished my text game in python. But, the only problem I have is that when I type in where to go it doesn't go to that place. It goes to the place under the start not what the user inputted in... I don't see any problems with my if and elif statements but don't know where the problem is... In fact i re-did all the elif and if statements but still have the same problem...
# This displays the map for the user to follow to play the game in another window.
from tkinter import*
window = Tk()
# Name of the window that the map is displayed in.
window.title('The Map')
# Size of the window
canvas = Canvas(window, width = 500, height = 500)
canvas.pack()
# This is the file path of the map.
my_image=PhotoImage(file='C:\\Users\\Oscar\\Desktop\\Bangor Uni S2\\ICP-1025-0 Intro to Intelligent Systems 201718\\Lab4\\map.png')
# Opens the window with the map.
canvas.create_image(0,0, anchor = NW, image=my_image)
# Welcome sign to the game.
def displayIntro():
print (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(" ~ ~")
print(" ~ ~")
print(" ~ WELCOME TO MILK ~")
print(" ~ ~")
print(" ~ MADE BY: ~")
print(" ~ OSCAR TSANG ~")
print(" ~ ~")
print (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("")
print("")
intropic()
print("")
def intro():
print()
print(" This is the game MILK!!! ")
print("")
print(" You are Farmer Bob and your objective is to find the lost milk ")
print("")
print(" The lost milk is located somewhere on the map ")
print("")
print("Yo would need to use the map to navigate around the farm to find the precious milk ")
print("")
print(" Only you can find the precious milk ")
print("")
print(" You will be in a farm that you will have to navigate through to get to the milk ")
print("")
print(" You will have no help ")
print("")
print(" There are 9 possibility where the milk can be, but only the map has the answer ")
print("")
print(" Now goodluck in finding the milk... ")
print("")
print("")
print("")
print("")
print("")
print("")
#These are all the nodes to the game milk.
# The starting point of the game.
def start():
print(" You are at the start of the game MILK ")
print("Now you will have to use the map to navigate around the farm to find the almighty, godly, delicious MILK!!!!")
way_picked = input("Do you want to go to the Shop 1, Field , Shop , House , Farm , Dead end, Shop 1, Field 1 ???? > ")
# This is where the user picks where they want to go within the game.
if way_picked.lower() == "Shop 1":
shop1()
elif way_picked.lower() == "Field":
field()
elif way_picked.lower() == "Shop":
shop()
elif way_picked.lower() == "House":
house()
elif way_picked.lower() == "Farm":
farm()
elif way_picked.lower() == "Dead End":
deadend()
elif way_picked.lower == "Field 1":
field1()
else:
print("")
print("")
print("")
print("")
print("")
# The field node in the game.
def field():
cow()
print( "This isn't where the milk is... ")
print("This is an empty field full of grass with sheeps and cows munching on it 24/7")
print("")
print("")
way_picked = input(" Start, Shop 1 or Shop ??? > ")
# This is where the user picks where they want to go when they they reach field node.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Shop":
shop()
elif way_picked.lower == "Shop 1":
shop1()
else:
print("")
print("")
print("")
print("")
print("")
# The shop node in the game where Bob gets his eqipment from.
def shop():
print(" This isn't where the milk is... ")
print(" This is the farmers equipment shop.'.. ")
way_picked = input(" Start, Field or House ??? > ")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "House":
House()
elif way_picked.lower == "Field ":
field()
else:
print("")
print("")
print("")
print("")
print("")
# House node of the game where Bob lives.
def house():
print(" This is your house.... ")
print(" This isn't where the milk is... ")
print(" Look harder Bob!!!! ")
way_picked = input(" Start , Shop , Farm ??? > ")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Shop":
shop()
elif way_picked.lower == "Farm":
farm()
else:
print("")
print("")
print("")
print("")
print("")
# The farm node of the game
def farm():
print(" This is the farm.... ")
print(" The milk is nowhere to be seen ")
print(" This is where all the equipment is held... ")
way_picked = input(" Start , Dead End or House??? >")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Dead End":
deadend()
elif way_picked.lower == "House":
house()
else:
print("")
print("")
print("")
print("")
print("")
# This is the dead end of the game where the player has to go back to the previous spot.
def deadend():
print(" This is DEAD END!!!!! ")
print(" You need to go back where you was.... ")
way_picked = input(" Start or Farm ??? >")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Farm":
farm()
else:
print("")
print("")
print("")
print("")
print("")
# This is another field kind of like a duplicate to the first field.
def field1():
print(" This is another field!!!!! ")
print(" Not where the milk is!!!!!!!! ")
way_picked = input(" Start , Shop 1 or Road ??? >")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Shop 1":
shop1()
elif way_picked.lower == "Road":
road()
else:
print("")
print("")
print("")
print("")
print("")
def shop1():
print(" This is Dominos Pizza ")
print(" This is not where the milk is Bob!!!!!! ")
print("Where do you want to go next:")
way_picked = input(" Start , Field 1 or Field ??? >")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Start":
start()
elif way_picked.lower() == "Field":
field()
elif way_picked.lower == "Field 1":
field1()
else:
print("")
print("")
print("")
print("")
print("")
# The main road by Bob's farm.
def road():
print(" This is the main road and certainly not whrer the milk is kept ")
print(" Try again Bob ")
print("You are very near the milk...")
print("Do you go forward or backwards????")
way_picked = input("Forwards or Backwards?? >")
# This is where the user picks where they want to go when they they reach shop1.
if way_picked.lower() == "Forwards":
start()
elif way_picked.lower() == "Backwards":
road()
else:
print("")
print("")
print("")
print("")
print("")
# This is where the milk is. When the player reaches this node they win the game.
def milk():
milk1()
print(" Finally!!! Bob you have found the missing milk!!!!! ")
print(" WELL DONE BOB!!!!!!!! ")
print(" NOW DRINK THE MILK BOB!!!!!! ")
mario()
# Prints out all the 10 nodes in my game. And is only using testing to see if every single node prints out as wanted.
displayIntro()
intro()
start()
Upvotes: 1
Views: 62
Reputation: 51683
You are converting the inputted text .lower()
and you compare against text that has uppercase in it - this wont work. Fix like this:
if way_picked.lower() == "shop 1":
shop1()
elif way_picked.lower() == "field":
field()
elif way_picked.lower() == "shop":
shop()
elif way_picked.lower() == "house":
house()
elif way_picked.lower() == "farm":
farm()
elif way_picked.lower() == "dead end":
deadend()
elif way_picked.lower() == "field 1":
field1()
where ever you compare. You might also want to use a .strip()
to ensure leading/trainling whitespaces are removed as well before comparing them.
Also: please read: How to debug small programs (#1) to get tips how to debug your own programms in a way to find your own errors faster then posting on SO.
One solution would be to make a small function that compares for you:
def comp(textIn, myOption):
"""Returns True if textIn.strip().lower() equals myOption.strip().lower()"""
return textIn.strip().lower() == myOption.strip().lower()
# The starting point of the game.
def start():
print(" You are at the start of the game MILK ")
print("Now you will have to use the map to navigate around the farm to find the almighty, godly, delicious MILK!!!!")
way_picked = input("Do you want to go to the Shop 1, Field , Shop , House , Farm , Dead end, Shop 1, Field 1 ???? > ")
# This is where the user picks where they want to go within the game.
if comp(way_picked,"Shop 1"):
shop1()
elif comp(way_picked,"Field"):
field()
elif comp(way_picked, "Shop"):
shop()
# etcerea ....
The function comp(userInput,"option")
will take care of stripping & lowering and comparing and simply return True/False
Edit: other way to deal with this: create dictionary that holds a key (the user input) and the function to call (as value):
def func1():
print("F1")
def func2():
print("F2")
def func3():
print("F3")
def func4():
print("F4")
# function are first class citizens in python, you can store them as
# value (without parentheses) to be called later
options = { "f1" : func1, "f2" : func2, "f3" : func3, "f4" : func4}
while True:
userInput = input("F1 to F4:\n\t").strip().lower() # make input trimmed and lower
if userInput in options:
options[userInput] () # execute the stored function (use value + () to execute)
else:
print("\nWrong input!\n")
Upvotes: 1