Python文本冒险游戏无法正常工作

时间:2017-03-18 13:52:05

标签: python

我一直试图让这个Python代码工作大约一个小时,但我似乎无法修复它。我前几天进入Python,所以如果这很容易,那就是原因。

def firstChoice():
    time.sleep(2)
    print('You come across a path, it splits at the end.')
    time.sleep(1)
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n')
    checkChoice()

def checkChoice():
#    correct
    if choice=='1' or choice=='2':
        correct_choice=randint(1,2)
        if choice==correct_choice:
            correct=True
    if choice!='1' or choice!='2':
        print('You decide to not take a path, and you die due to random circumstances.')
        time.sleep(1)
        print('Take a path next time, or at least take it correctly.')
        failScreen()

I've imported everything necessary (time and random)

EDIT: Here's the whole code.

def firstChoice():
    time.sleep(2)
    print('You come across a path, it splits at the end.')
    time.sleep(1)
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n')
    checkChoice()

def checkChoice():
#    correct
    if choice=='1' or choice=='2':
        correct_choice=randint(1,2)
        if choice==correct_choice:
            correct=True
    if choice!='1' or choice!='2':
        print('You decide to not take a path, and you die due to random circumstances.')
        time.sleep(1)
        print('Take a path next time, or at least take it correctly.')
        failScreen()

2 个答案:

答案 0 :(得分:0)

我猜你总是在failScreen结束,因为你的第二个if语句正在使用!=1 or !=2,这将导致始终true ...将其更改为{ {1}}看看它是否有帮助。

我还不确定and功能中是否显示choice。在Java中,您需要在function-bodys

之外声明变量

答案 1 :(得分:0)

首先,如果你正在使用python2.7,你需要知道输入()如果你通过控制台给它一个数字,这个函数转换为int,那么你需要检查值,如if {{1或者只是将输入更改为choice == 1(这是最好的方法),同样在raw_input()中您需要放置if choice!='1' and choice!='2':然后避免大量检查或意外值。

如果您使用的是python3,则raw_input是新的输入函数,因此您无需更改

并且,最后如果你想使用raw_input()或者你正在使用python3,你需要将随机函数更改为else:,因为你正在将str与int进行比较,这里你有你的代码改变for python2.7:

random.choice('1','2')