Python文本冒险游戏无法运行

Python文本冒险游戏无法运行,python,Python,我已经试着让这个Python代码运行了大约一个小时,但似乎无法修复它。前几天我学习了Python,如果这很容易,那就是原因。 我已经导入了所有必要的内容(时间和随机) 编辑:这是全部代码 import random import time choice=0 def introDisplay(): print('This is the pre-game story.') time.sleep(1) print('It lasts for 5 lines.')

我已经试着让这个Python代码运行了大约一个小时,但似乎无法修复它。前几天我学习了Python,如果这很容易,那就是原因。

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

编辑:这是全部代码

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)? \n') 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. \n') 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()
随机输入 导入时间 选择=0 def introDisplay(): 打印('这是游戏前的故事') 时间。睡眠(1) 打印('持续5行') 时间。睡眠(1) 打印('当你被纵火时,修复此') 时间。睡眠(1) 打印('谢谢') 时间。睡眠(1) 打印(“您,从2017年3月18日起”) 打印() 第一选择() def firstChoice(): 时间。睡眠(2) print('您遇到一条路径,它在末尾分裂') 时间。睡眠(1) choice=input('您选择哪条路径,左边的路径(1)还是右边的路径(2)?\n') 选择(选择) def checkChoice(选项): 正确=错误 如果选项=='1'或选项=='2': 正确的选择=random.randint(1,2) 如果选择==正确的选择: 正确的 如果选择1'和选择!='2': print('你决定不走一条路,然后由于随机情况而死亡') 时间。睡眠(1) 打印('下次选择路径,或者至少正确选择路径') 故障屏幕() def failScreen(): 重新启动=真 打印('您失败了') 打印('是否要重试?') restart1=input('Y或Y=Yes.N或N=No.\N') 如果restart1=='y'或restart1=='y': 重新启动=真 如果restart1=='n'或restart1=='n': 重新启动=错误 如果重新启动1!='n'或重新启动!='N'或重新启动!='y'或重新启动!='Y': 故障屏幕() 如果重新启动==True: introDisplay() 如果重新启动==False: 退出() introDisplay()
我猜您总是在
故障屏幕中结束,因为您的第二个
if
语句正在使用
=一个或多个=2
这将导致始终
为真
。。。将其更改为
,以查看是否有帮助


另外,我不确定在
checkChoice
功能中是否可以看到
choice
。在Java中,首先需要在函数体之外声明变量,如果您使用python2.7,您需要知道input(),如果您通过控制台给它一个数字,这个函数将转换为int,那么您需要检查值,比如if
choice==1
,或者只需将input更改为
raw\u input()
(这是最好的方法),同样在
if choice!='1'和choice!='2':
中,您需要放入
else:
以避免大量检查或意外值

如果您使用的是python3 raw_input,那么它是新的输入函数,您不需要更改它

最后,如果您想使用raw_input()或使用python3,您需要将随机函数更改为
random.choice('1','2')
,因为您要将str与int进行比较,这里有您对python2.7所做更改的代码:

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)? \n')
    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. \n')
    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()

您需要将该选项作为参数传递给函数
def checkChoice(choice):
并且当您调用它时
checkChoice(choice)
。此外,您还需要在第二种情况下使用
else
,如果不起作用……请参阅,以获取有关构建更好(更负责)的问题与网站规则一致。另请参见--注意,我们关心的是问题本身,而不是遇到问题时编写的程序类型(我们要求的是具有相同问题的最短程序--最短程序可能不会是文本冒险)。如果你的问题集中在你正在编写的程序上,而不是你遇到的问题,那么它就集中在了错误的地方。所有这些都是有效的,但是原始输入导致了错误,但在将其更改为输入后,一切都正常。
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)? \n')
    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. \n')
    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()