繁体   English   中英

我的脚本在外壳程序上运行,但不在命令提示符下运行

[英]My script runs on the shell but not on the command prompt

我最近开始从一本书中学习python(由Al Sweigart用python发明自己的计算机游戏),并且我正尝试使用可以学习修改的练习内容,使它们更符合我的喜好。 无论如何,有一个我试图修改的脚本,而修改后的版本按照我喜欢的方式运行,当我尝试使用交互式外壳运行它时,我双击脚本图标以使其运行在命令行界面(到目前为止,我希望我使用的是正确的术语,因为我对编程尚不十分熟悉),它无法运行。 命令窗口打开,什么也没有发生。

这是在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()

我尝试从print(string [i],end ='')行中删除“ end =”部分,但它确实运行正常!(尽管结果很糟糕,因为每行键入1个字符!)

您认为问题是什么?如何在不使每行键入一个字符的情况下解决该问题?

谢谢你的时间! 法案

(Ps:在格式化帖子代码时,我只需要缩进尚未缩进的行,所以我认为在尝试复制代码时缩进可能会出现问题,因为某些行缺少缩进?无论如何,我希望这是没什么大不了)

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

读取功能应如下所示

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 ...]

优良作法(PEP8)在数学符号和条件运算符之间留有空格,如下所示

def chooseCave():
    [... code ...]
    i = 0
    [... code ...]
    while cave != '1' and cave != '2' and i <= 10:
        [... code ...]

PEP8的另一个好的做法是不超过79个最大行长度。 因此,当您的字符串很长时,不传递79个字符的一种方法是执行以下操作

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)

“读取”函数末尾的print()语句将打印新行。

没有print()的整个代码:

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

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()

好吧...我试图解决您的问题。 我用自己的方式编写了代码,但它在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,则可以使您的程序更有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM