【问题标题】:I'm making a text based adventuere game in python and trying to add a third option but it's not working我正在用 python 制作一个基于文本的冒险游戏并尝试添加第三个选项,但它不起作用
【发布时间】:2018-03-10 13:37:16
【问题描述】:

我正在尝试制作一个具有三个选择的简单文字冒险游戏。但我似乎无法弄清楚为什么这不起作用。

这是我一直在处理的代码:

#Text based medieval adventure game

#Imported libraries are required
import random
import time

def displayWelcome():
    print ("\nText adventure game\n")
    print ("Welcome to the medieval adventure game!")
    print ("You will have many choices through out this game")
    print ("Be sure to pick the right one")
    print ("          Good Luck!          ")

    answer = askYesNo("Would you like help with this program? (Y/N): ")

    if answer == "Y":
        helpForUser()
        time.sleep(3)

def askYesNo (question):
# This function will ask you a yes or no question and will keep asking until one is chosen.
# The following function is used to erase the variable response of any values
    response = None

    while response not in ("Y", "N"):

        response = input (question).upper()

    return response

def helpForUser():
#This function will show the user some helpful advice if they desire it
    print ("\n+++++++++++++++++++++++++++++++++++++++ Help +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    print ("This game is a adventure text based game set in the medieval times")
    print ("You will be asked multiple questions with a yes or no response")
    print ("you must answer the questions with the answers supplied to you suches as yes or no")
    print ("If you don't answer the q uestion with the given answers it will be repeated untill a valid response occurs")
    print ("The program can end when you choose")
    print ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def displayIntro():
    #Displays intro to game
    print ("\n It's been a rough day in the wild and you despratly need shelter")
    print ("There is currently a war going on and nowhere is safe")
    print ("But you intend to find somwhere with food and a bed but it wont be easy...")
    print ()



def choosePath(lower,middle,upper):
#This functions allows you to choose between multiple options
    path = 0
    while path < lower or path > upper:
        number = input("What path will you take(" + str(lower) + " - " + str(upper) + ")?: ")

    if number.isdigit():
        path = int (number)
    else:
        path = 0

    return path

def followPath(chosenPath):

    print ("you head down a long road\n")
    time.sleep(3)
    print ("You come across an abandoned campsite and decide to stay there for the night")
    time.sleep(3)
    print("You wake up to a sudden sound of voices and begin to realise that this campsite wasn't abandoned...")
    time.sleep(3)
    print("You start to freak out")
    time.sleep(3)

    if chosenPath == 1:
           print("You grab your sword out and decide to go out the tent")
           print ("Four well trained knights surround you")
           print ("They strike without any hesitation, you counter two knights trying to hit you from the front as two from behind stab you in the back")
           print ("The knights decide to burn your body and leave nothing left of you.\n")
    elif chosenPath == 2:
           print("You dart out of the tent and head for the exit")
           print("All the guards try and get you whilst shooting arrows")
           print("Several arrows hit you leaving you injured unable to run")
           print ("Suddenly a man with a giant axe appears before you and slices you head off in a clean swoop\n")
    else chosenPath == 3:
           print("You keep calm and decide to sneak past all the guards")
           print ("You're close to the exit when suddenly a guard notices you")
           print("He's about to call for back up when you dash right into his chest with your sword, leaving it in him and running out the campsite")
           print("You make it to a safe forest and decide to rest\n")




displayWelcome()
playAgain = "Y"
while playAgain == "Y":

    displayIntro()
    choice = choosePath()
    followPath(choice)
    playAgain = askYesNo ("Would you like to play again?")
print ("\nBye")

【问题讨论】:

  • 请将这篇冗长的散文缩减为一个最小(非)工作示例。
  • choice = choosePath() 中的 choosePath() 不带任何参数,而在函数定义中有 3 个参数。为什么会这样?

标签: python python-3.x adventure


【解决方案1】:

错误号一:第 63 行,否则应为 elif 或去掉条件“chosenPath == 3: 当前状态

    else chosenPath == 3:

它应该是什么样子

    elif chosenPath == 3:

    else:

另一个错误是选择初始化时什么都没有发生,因为您忘记输入“下、中、上”值。

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2016-08-25
    • 2013-07-08
    • 2016-01-08
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多