我的脚本在shell上运行,但不在命令promp上运行

2024-05-14 08:46:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试着从print(string[I],end='')行中删除“end=''”部分,它确实正常运行了!(尽管每行输入1个字符的结果很糟糕!)你知道吗

您认为问题出在哪里?如何在不使每行键入一个字符的情况下修复它?你知道吗

谢谢你的时间! 比尔

(Ps:在格式化文章的代码时,我只需要缩进那些还没有缩进的行,所以我想在复制代码时缩进可能有问题,因为有些行没有缩进?无论如何,我希望这不是一个问题)


Tags: orandofinyoutimedefsleep
3条回答

嗯。。。我试着解决你的问题。我用我的方式编写了您的代码,但它在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很简单和容易(至少对我来说)。如果你学习图形用户界面,你就能使你的程序更有趣。你知道吗

“read”函数末尾的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()

您需要导入系统并使用系统标准冲洗()函数以获取所需的字符流。你知道吗

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

在数学符号和条件运算符之间使用空格是一种很好的做法(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)

相关问题 更多 >

    热门问题