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

时间:2017-09-22 03:12:33

标签: python python-3.x shell command-line-interface

我最近开始从一本书中学习python(用Al Sweigart用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()

我尝试删除&#34; end =&#39;&#39;&#34;&#34;部分来自print(string [i],end =&#39;&#39;)行,它确实正常运行!(虽然结果非常糟糕,因为每行输入1个字符!)

您认为这个问题是什么?我怎样才能解决问题,而不是每行输入一个字符?

感谢您的时间! 比尔

(Ps:在格式化帖子的代码时,我不得不缩进尚未缩进的行,所以我认为在尝试复制代码时可能存在缩进问题,因为某些行缺少缩进?无论如何我希望这不是问题)

3 个答案:

答案 0 :(得分:1)

您需要导入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)

答案 1 :(得分:0)

&#34;结束时的print()声明&#34;功能将打印一个新行。

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

答案 2 :(得分: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,您将能够使您的程序更有趣。

相关问题