2017-07-23 32 views
0

我是一名初学者程序员。我想创建一个用户输入影响游戏过程的游戏。我有一种卡住的一开始。python 3.x中基于文本的探险帮助

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()运行,但它的确如此,而不是退出循环而是直接回到那个循环,并询问玩家是否想再次醒来,我没有想法如何解决这个问题。

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

谢谢你的时间,并希望你的答案。

回答

0

下面的代码应该可以工作。
1.我将“choice = input”(“你是1:回去睡觉还是2:起床”)行移动到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") 
+0

谢谢你这是工作......所以为了将来的参考,我需要添加一个新的函数调用后,如果它的内部循环中的break语句? – Question

+0

如果您希望立即退出循环并达到所需条件,则需要添加break语句。在你的代码中,所需的条件是一个“走哪条路”的决定。一旦函数whichWay()返回它的输出,就不需要继续for循环,因此它必须退出。 –

+0

好的。再次感谢你 – Question