【问题标题】:text-based adventure help in python 3.xpython 3.x 中基于文本的冒险帮助
【发布时间】:2017-07-23 17:49:54
【问题描述】:

我是一名初级程序员。我想创建一个用户输入会影响游戏进程的游戏。一开始我有点卡住了。

def displayIntro():
    print("You wake up in your bed and realize today is the day your are going to your friends house.")
    print("You realize you can go back to sleep still and make it ontime.")

def wakeUp():
    sleepLimit = 0
    choice = input("Do you 1: Go back to sleep or 2: Get up ")
    for i in range(3):
        if choice == '1':
            sleepLimit += 1
            print("sleep")
            print(sleepLimit)
                if sleepLimit == 3:
                    print("Now you are gonna be late, get up!")
                    print("After your shower you take the direct route to your friends house.")

        elif choice == '2':
            print("Woke")
            whichWay()

        else:
            print("Invalid")

def whichWay():
    print("After your shower, you decide to plan your route.")
    print("Do you take 1: The scenic route or 2: The quick route")
    choice = input()
    if choice == 1:
        print("scenic route")
    if choice == 2:
        print("quick route")



displayIntro()
wakeUp()

我有一些错误,我试图自己解决它们,但我很挣扎。

1) 我只希望玩家能够再次入睡 3 次,第三次我希望出现一条消息并运行另一个功能(还没有实现)。

2) 如果玩家决定醒来,我希望 whichWay() 运行,但它不会退出 for 循环,而是直接返回该循环并询问玩家是否想再次醒来,我不知道如何解决这个问题。

3) 有没有更好的方法来制作这样的游戏?

感谢您的宝贵时间并希望您的回答。

【问题讨论】:

    标签: python-3.x function loops break adventure


    【解决方案1】:

    下面的代码应该可以工作。
    1. 我将 'choice = input("Do you 1: Go back to sleep or 2: Get up")' 移动到 for 循环中。
    2.我在elif块的末尾添加了一个break语句。

    def wakeUp():
    sleepLimit = 0
    
    for i in range(3):
        choice = input("Do you 1: Go back to sleep or 2: Get up ")
        if choice == '1':
            sleepLimit += 1
            print("sleep")
            print(sleepLimit)
            if sleepLimit == 3:
                print("Now you are gonna be late, get up!")
                print("After your shower you take the direct route to your friends house.")
    
        elif choice == '2':
            print("Woke")
            whichWay()
            break
    
        else:
            print("Invalid")
    

    【讨论】:

    • 谢谢你这正在工作......所以为了将来参考,如果它在循环内,我需要在新函数调用之后添加一个 break 语句?
    • 如果您希望在满足所需条件时立即退出循环,您需要添加一个 break 语句。在您的代码中,所需的条件是决定“走哪条路”。一旦 whichWay() 函数返回它的输出,就不需要继续 for 循环,因此它必须退出。
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多