簡體   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