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

Python text-adventure game not working

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)?
'
)
    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()

我已经导入了所有必要的东西(时间和随机)

编辑:这是整个代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import random
import time

choice=0

def introDisplay():
    print('This is the pre-game story.')
    time.sleep(1)
    print('It lasts for 5 lines.')
    time.sleep(1)
    print('When you can be arsed, fix this.')
    time.sleep(1)
    print('Thanks,')
    time.sleep(1)
    print('You, from 18/3/17')
    print()
    firstChoice()

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)?
'
)
    checkChoice(choice)

def checkChoice(choice):
    correct=False
    if choice=='1' or choice=='2':
        correct_choice=random.randint(1,2)
        if choice==correct_choice:
            correct=True
    if choice!='1' and 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()




def failScreen():
    restart=True
    print('You have failed.')
    print('Do you want to retry?')
    restart1=input('Y or y = Yes. N or n = No.
'
)
    if restart1=='y' or restart1=='Y':
        restart=True
    if restart1=='n' or restart1=='N':
        restart=False
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y':
        failScreen()
    if restart==True:
        introDisplay()
    if restart==False:
        exit()

introDisplay()


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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import random
import time

choice=0

def introDisplay():
    print('This is the pre-game story.')
    time.sleep(1)
    print('It lasts for 5 lines.')
    time.sleep(1)
    print('When you can be arsed, fix this.')
    time.sleep(1)
    print('Thanks,')
    time.sleep(1)
    print('You, from 18/3/17')
    print()
    firstChoice()

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

def checkChoice(choice):
    correct=False
    if choice=='1' or choice=='2':
        correct_choice=random.choice(['1','2'])
        if choice==correct_choice:
            correct=True
            print('You chose the correct path')
        else:
            print('You dont chose the correct path')
    else:
        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()




def failScreen():
    restart=True
    print('You have failed.')
    print('Do you want to retry?')
    restart1=input('Y or y = Yes. N or n = No.
'
)
    if restart1=='y' or restart1=='Y':
        restart=True
    if restart1=='n' or restart1=='N':
        restart=False
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y':
        failScreen()
    if restart==True:
        introDisplay()
    if restart==False:
        exit()

introDisplay()


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

我也不确定checkChoice功能中是否可以看到choice。在Java中,您需要在function-bodys之外声明变量