Python游戏不会去我想去的地方

时间:2018-03-26 12:14:44

标签: python python-3.x

我已经在python中完成了我的文本游戏。但是,我唯一的问题是,当我输入去哪里时,它不会去那个地方。它在开始的地方而不是用户输入的内容......我没有看到我的if和elif语句有任何问题但是不知道问题出在哪里...事实上我是-did所有elif和if语句但仍然有同样的问题......

# 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()

1 个答案:

答案 0 :(得分:1)

您正在转换输入的文本.lower(),并将其与其中包含大写的文本进行比较 - 这不起作用。修复如下:

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()
你比较

哪里。您可能还想使用.strip()来确保在比较它们之前删除引导/训练空格。

另外:请阅读:How to debug small programs (#1)以获取有关如何调试自己的程序的提示,以便更快地找到自己的错误,然后在SO上发布。

一种解决方案是制作一个可以比较的小功能:

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 ....  

函数comp(userInput,"option")将负责剥离&降低和比较,只需返回True/False

编辑:处理此问题的其他方法:创建包含密钥(用户输入)的字典和要调用的函数(作为值):

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")