Python游戏不去我想去的地方

Python游戏不去我想去的地方,python,python-3.x,Python,Python 3.x,我已经用python完成了我的文本游戏。但是,我唯一的问题是,当我输入去哪里时,它不会去那个地方。它去的地方下开始不是什么用户输入的。。。我看不出if和elif语句有任何问题,但不知道问题出在哪里。。。事实上,我重新做了所有的elif和if语句,但仍然有相同的问题 # This displays the map for the user to follow to play the game in another window. from tkinter import* window = Tk(

我已经用python完成了我的文本游戏。但是,我唯一的问题是,当我输入去哪里时,它不会去那个地方。它去的地方下开始不是什么用户输入的。。。我看不出if和elif语句有任何问题,但不知道问题出在哪里。。。事实上,我重新做了所有的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()

您正在转换输入的文本
.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()
来确保在比较前导空格/列空格之前也删除它们

另外:请阅读:获取如何调试您自己的程序的技巧,以便更快地找到您自己的错误,然后发布在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(用户输入,“选项”)
将负责剥离、降低和比较,并简单地返回
True/False


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


@XanderPowel请参见
Start()
函数的示例-您需要在任何地方将用户输入与一些“选项”进行比较,以便分支到程序的不同部分。也许更好的方法是制作一本字典,以“shop1”为键,函数为值。因此,您可以使用它来比较-但这样更容易实现…@XanderPowel修复了
trim()
strip()
[
trim()
是c#-很抱歉犯了这个错误],并添加了一个示例,说明如何将函数作为值存储在
dict
中并调用它。你可以复制粘贴下面的代码片段并进行实验。如果我再问你一个问题可以吗@Patricksure——你可能想提出一个新问题,这样其他人也可以回答。注释并不是发布代码的好方法——它不是格式化的,这对python来说很重要
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")