简体   繁体   English

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

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

I've recently started learning python from a book (invent your own computer games with python by Al Sweigart) and I'm trying to use stuff that I learn to modify exercises that I've done to make them more to my liking. 我最近开始从一本书中学习python(由Al Sweigart用python发明自己的计算机游戏),并且我正尝试使用可以学习修改的练习内容,使它们更符合我的喜好。 Anyway, there was one such script which I tried to modify and while the modified version runs just the way I like it when I'm trying to run it using the interactive shell, when I double click the script icon to have it run on the command line interface(I hope I'm using the correct terminology so far cause I'm not really familiar with programming as of yet) it doesn't run. 无论如何,有一个我试图修改的脚本,而修改后的版本按照我喜欢的方式运行,当我尝试使用交互式外壳运行它时,我双击脚本图标以使其运行在命令行界面(到目前为止,我希望我使用的是正确的术语,因为我对编程尚不十分熟悉),它无法运行。 The command window opens and nothing happens. 命令窗口打开,什么也没有发生。

Here's the original script that runs on both the shell and the command line: 这是在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()

And this is the modified version (I wanted the text to appear as being typed on the go to make it look more immersive: 这是修改后的版本(我希望文本可以随时随地显示,以使其看起来更加身临其境:

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

I tried removing the "end=''" part from the print(string[i],end='') line and it did run normally!(Although with terrible results as it typed 1 character per line!) 我尝试从print(string [i],end ='')行中删除“ end =”部分,但它确实运行正常!(尽管结果很糟糕,因为每行键入1个字符!)

What do you think is the issue and how can I fix it without making it type a single character per line? 您认为问题是什么?如何在不使每行键入一个字符的情况下解决该问题?

Thanks for your time! 谢谢你的时间! Bill 法案

(Ps: during formatting the code for the post, I had to only indent lines that were not already intdented, so I think there might be an issue with indentation when trying to copy the code as some lines lack indentation? Anyway I hope this is not an issue) (Ps:在格式化帖子代码时,我只需要缩进尚未缩进的行,所以我认为在尝试复制代码时缩进可能会出现问题,因为某些行缺少缩进?无论如何,我希望这是没什么大不了)

You need to import sys and use sys.stdout.flush() function to get the flow of characters you want. 您需要导入sys并使用sys.stdout.flush()函数来获取所需的字符流。

The read function should look like this 读取功能应如下所示

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

It is good practice (PEP8) to have spaces between math symbols and conditional operators like the following 优良作法(PEP8)在数学符号和条件运算符之间留有空格,如下所示

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

Another PEP8 good practice is to not pass 79 maximum line length. PEP8的另一个好的做法是不超过79个最大行长度。 So, when you have a really long string, one way to not pass the 79 characters is to do the following 因此,当您的字符串很长时,不传递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)

The print() statement at the end of "read" function will print a new line. “读取”函数末尾的print()语句将打印新行。

The whole code without the 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()

Well... I tried to solve your problem. 好吧...我试图解决您的问题。 I wrote your code in my way but it works in the python shell but not in the command prompt window. 我用自己的方式编写了代码,但它在python shell中有效,但在命令提示符窗口中不起作用。 However, here is my code: 但是,这是我的代码:

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)

I think you should learn GUI(Graphical User Interface) using tkinter (because using tkinter is simple and easy (at least for me)). 我认为您应该使用tkinter来学习GUI(图形用户界面)(因为使用tkinter既简单又容易(至少对我而言))。 If you learn GUI, you'll be able to make your programs more interesting. 如果您学习GUI,则可以使您的程序更有趣。

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

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