Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的脚本在shell上运行,但不在命令提示符下运行_Python_Python 3.x_Shell_Command Line Interface - Fatal编程技术网

Python 我的脚本在shell上运行,但不在命令提示符下运行

Python 我的脚本在shell上运行,但不在命令提示符下运行,python,python-3.x,shell,command-line-interface,Python,Python 3.x,Shell,Command Line Interface,我最近开始从一本书中学习python(AlSweigart的《用python发明你自己的电脑游戏》),我正在尝试使用我学到的东西来修改我所做的练习,使它们更符合我的喜好。 无论如何,有一个这样的脚本我试图修改,当我试图使用交互式shell运行它时,当我双击脚本图标使它在命令行界面上运行时,修改后的版本以我喜欢的方式运行(我希望到目前为止我使用的是正确的术语,因为我还不太熟悉编程)它没有运行。命令窗口打开,什么也没有发生 以下是在shell和命令行上运行的原始脚本: import random i

我最近开始从一本书中学习python(AlSweigart的《用python发明你自己的电脑游戏》),我正在尝试使用我学到的东西来修改我所做的练习,使它们更符合我的喜好。 无论如何,有一个这样的脚本我试图修改,当我试图使用交互式shell运行它时,当我双击脚本图标使它在命令行界面上运行时,修改后的版本以我喜欢的方式运行(我希望到目前为止我使用的是正确的术语,因为我还不太熟悉编程)它没有运行。命令窗口打开,什么也没有发生

以下是在shell和命令行上运行的原始脚本:

import random
import time

def displayIntro():
    print('''You are in a land full of dragons. In front of you, you see two caves. in one cave, the dragon is friendly and will share his treasure with you. the other dragon is greedy and hungry and will eat you on sight.''')
    print()


def chooseCave():
    cave=''

    while cave != '1' and cave!='2':
        print('Which cave will you go into?(1 or 2)')
        cave=input()
    return cave


def checkCave(chosenCave):
    print('you approach the cave...')
    time.sleep(2)
    print('it\'s dark and spooky')
    time.sleep(2)
    print('a large dragon jumps out in front of you! he opens his jaws and...')
    print()
    time.sleep(2)
    friendlyCave=random.randint(1,2)

    if chosenCave==str(friendlyCave):
        print('gives you his treasure!')
    else:
        print('gobbles you down in 1 bite!')

playAgain = 'yes'

while playAgain=='yes' or playAgain=='y':
    displayIntro()
    caveNumber=chooseCave()
    checkCave(caveNumber)
    print('Do you want to play again?(yes or no)')
    playAgain=input()
这是修改后的版本(我希望文本显示为在移动中键入,使其看起来更具沉浸感:

import random
import time


def read(string):
    i=0
    while i<len(string):
        print(string[i],end='')
        time.sleep(0.05)
        if string[i]=='.':
            time.sleep(0.5)
        i=i+1
    print('')


def displayIntro():
    intro='''You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight.'''
    i=0
    read(intro)


def chooseCave():
    cave=''
    i=0
    question='Which cave will you go into? (1 or 2)'
    j=0
    print('')
    read(question)
    print('')

    while cave != '1' and cave != '2' and i<=10:
        cave = input()
        i=i+1
    return cave


def checkCave(chosenCave):
    approach='You approach the cave...'
    j=0
    read(approach)
    print()

    spooky='It\'s dark and spooky...'
    j=0
    read(spooky)

    time.sleep(1)
    print()
    print('\nA large dragon jumps out in front of you! He opens his jaw and...')
    time.sleep(1.5)

    friendlyCave=random.randint(1,2)

    if chosenCave == str(friendlyCave):
        print('Gives you his treasure!')
    else:
       print('Gobbles you down in one bite!')

playAgain='yes'

while playAgain=='yes' or playAgain== 'y':
    displayIntro()
    caveNumber=chooseCave()
    checkCave(caveNumber)
    print('Do you want to play again? (yes or no)')
    playAgain = input()
随机导入
导入时间
def读取(字符串):
i=0
而“read”函数末尾的print()语句将打印新行

没有打印()的整个代码:

随机导入
导入时间
def读取(字符串):
i=0

我试图解决你的问题。我用我的方式编写了你的代码,但它在python shell中工作,但在命令提示符窗口中不工作。不过,下面是我的代码:

import random as rnd
import time
import msvcrt

def read(string):
    for each in string:
        print(each, end="")
        time.sleep(0.05)

def displayIntro():

    intro="You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight."

    read(intro)



def chooseCave():
    cave=''
    print()

    while cave != '1' and cave!='2':

        read('Which cave will you go into?(1 or 2): ')

        cave=input()



    return cave


def checkCave(chosenCave):

    read('you approach the cave...')
    print()
    time.sleep(2)

    read("it's dark and spooky")
    print()
    time.sleep(2)

    read('a large dragon jumps out in front of you! he opens his jaws and...')
    print()
    print()

    time.sleep(2)



    friendlyCave=rnd.randint(1,2)



    if chosenCave==str(friendlyCave):

        read('gives you his treasure!')

    else:

        read('gobbles you down in 1 bite!')

    print()




playAgain="yes"
while playAgain=="yes" or playAgain=="y":
    displayIntro()
    caveNumber=chooseCave()
    checkCave(caveNumber)
    read("Do you want to play again? (yes/y or no/n): ")
    playAgain=input()
    playAgain=playAgain.lower()

print("Press any key to continue...")
while not msvcrt.kbhit():
    time.sleep(0.1)

我认为您应该使用tkinter学习GUI(图形用户界面)(因为使用tkinter简单易行(至少对我来说是这样))。如果您学习GUI,您将能够使您的程序更有趣。

您需要导入sys并使用sys.stdout.flush()函数来获得所需的字符流

read函数应该如下所示

import random
import time
import sys


def read(string):
    i = 0
    while i < len(string):
        print(string[i], end='')
        time.sleep(0.05)
        if string[i] == '.':
            time.sleep(0.5)
        # flush stdout after sleep
        sys.stdout.flush()
        i = i + 1
    print('')

[... rest of the code ...]

你能用适当的缩进来写代码吗?对不起,我添加了正确的缩进(我想)。不,您没有。您需要为代码的每行添加4个空格以进行适当的缩进。忘记了最后4行,只是仔细检查并修复了它们!但是我想用read函数一次打印一个字符,间隔很小,使它看起来像是有人在键入文本,而不是让它出现在文本中Stantally。它正常工作,就像我在尝试使用python shell运行时喜欢的那样,但当我双击脚本图标时就不正常了。好的,明白了。编辑了答案。这将像键入一样打印文本。但不确定双击:)
import random
import time
import sys


def read(string):
    i = 0
    while i < len(string):
        print(string[i], end='')
        time.sleep(0.05)
        if string[i] == '.':
            time.sleep(0.5)
        # flush stdout after sleep
        sys.stdout.flush()
        i = i + 1
    print('')

[... rest of the code ...]
def chooseCave():
    [... code ...]
    i = 0
    [... code ...]
    while cave != '1' and cave != '2' and i <= 10:
        [... code ...]
def displayIntro():
    intro = ('You are in a land full of dragons. In front of you, you see two '
             'caves. In one cave, the dragon is friendly and will share his '
             'treasure with you. The other dragon is greedy and hungry, and '
             'will eat you on sight.')
    read(intro)