繁体   English   中英

文字冒险游戏-python

[英]Text adventure game-python

我是编码新手。 我已经开始了这个编码,当我运行代码时它一直给我一个错误。 你能告诉我我的代码有什么问题吗?

import time
import random

response = []
decision_list = ["1", "2"]
print(random.choice(decision_list))


def print_pause(message_to_print):
    print(message_to_print)
    time.sleep(1)


def valid_input(prompt, option1, option2):
    while True:
        response = input(prompt).lower()
        if 1 == response:
            break
        elif 2 == response:
            break
    return response


def intro():
    print_pause("You find yourself driving down a long road alone to a cabin.")
    print_pause("You have heard on the radio that it is a bear on the loose.")
    print_pause("All of a sudden your car slows down by the dark woods.")
    print_pause("You become terrified, but you see a house nearby for help.")
    print_pause("You come closer to the house, it is a big shed to the left.")
    print_pause("In your pocket, you have a pocketknife.")


def decision():
    response = valid_input("Enter 1 to knock on the door of the house."
                           "Enter 2 to walk into the shed."
                           "What is you decision?"
                           "Please enter 1 or 2.")
    if "1" in response:
        print_pause("You have arrived to the door of the house.")
        print_pause("Before knocking, the door starts to creek open.")
        print_pause("The bear is standing in the doorway.")
        print_pause("The bear attacks.")
        print_pause("With your pocket knife, you cannot handle the bear.")

    elif "2" in response:
        print_pause("You walk towards the shed.")
        print_pause("You look around and you saw a gun.")
        print_pause("Next to the gun are bullets.")
        print_pause("You have a weapon for protection besides your pocketknife.")
        print_pause("You walk back to the house.")
        play_again()


def action():
    response = valid_input("Would you like to 1 fight or 2 run away?"
                           " Please enter 1 or 2.")
    if "1" in response:
        print_pause("The bear attacks and you have taken out the gun.")
        print_pause("You begin to shoot the bear.")
        print_pause("You have defeated the bear!!")
    elif "2" in response:
        print_pause(" You run back to your car where it is safe.")
    elif "1" in response:
        print_pause("You begin fighting back.")
        print_pause("The pocketknife is not helpful.")
        print_pause("The bear had defeated you!!")


def play_again():
    response = valid_input("Would you like to play again?"
                           "Please say 'yes' or 'no'.\n",
                           "yes", "no")
    if "yes" == response:
        print_pause("Great! Restarting game.")
        play_drive()
    elif "no" == response:
        print_pause("Thanks for playing!! Goodbye!")
        exit(0)


def play_drive():
    intro()
    decision()
    action()
    play_drive()

问题出在 function 决定的第一行。 在那里,您只需一个参数即可调用 function valid_input。 我不确定你是否需要它,但你用昏迷将不同的 arguments 分开。 希望我对你有帮助:)

你没有提到你的错误是什么,但我可以指出一些不起作用的事情。

您定义valid_input(prompt, option1, option2) ,这意味着需要三个参数。 但是每次调用valid_input()时只需传递一个文本,这样就会产生错误。 但是稍后在代码中,您永远不会对任何内容使用option1option2 ,因为您在if..elif中有硬编码12 还要记住,输入会将值捕获为字符串,然后将其与两个整数进行比较。 所以更好:

def valid_input(prompt, option1 = "1", option2 = "2"):
    while True:
        response = input(prompt).lower()
        if option1 == response or option2 == response:
            return response

response = valid_input("Enter 1 to knock on the door of the house."
    "Enter 2 to walk into the shed."    
    "What is you decision?"
    "Please enter 1 or 2.")

更新:由于您尝试调用相同的 function 有时将选项传递为“是”/“否”,有时您想与“1”和“2”进行比较,您可以做的是保留“1”和“2” " 作为默认值,如果您使用“yes”和“no”调用valid_input ,这些值将被忽略

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM