Python正在打印所有的东西,我只希望它打印最后的麻木

2024-09-29 21:43:26 发布

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

以下是我目前的代码:

import sys
import time
import random 
from sys import *
from random import *
from time import sleep

def intro():
    global lives
    lives = 2
    intro1() def intro1():
    global lives
    text1 = 'please enter (Yes or No)'
    for x in text1:
            print (x, end='')
            sys.stdout.flush()
            sleep(0.007)
    print()
    answer = input()
    if 'yes' in answer:
        lives = lives + 1
        text1 = 'Thank you'
        text2 = '+ 1 life'
        text3 = lives
        text4 = ' lives now.'
        for x in text1:
            print (x, end='')
            sys.stdout.flush()
            sleep(0.007)
        print()
        for x in text2:
            print (x, end='')
            sys.stdout.flush()
            sleep(0.007)
        print()
        for x in range(text3):
            print (x, end='')
            sys.stdout.flush()
            sleep(0.007)
        for x in text4:
            print (x, end='')
            sys.stdout.flush()
            sleep(0.007)
        print()
        intro1()
    if 'no' in answer:
        lives = lives - 1
        if lives <= 0:
            text1 = 'Out of lives. Game Over.'
            for x in text1:
                print (x, end='')
                sys.stdout.flush()
                sleep(0.007)
            end()
        elif lives >= 0:
            text1 = 'That\'s rude'
            text2 = '- 1 life!'
            text3 = lives
            text4 = ' lives left'
            for x in text1:
                print (x, end='')
                sys.stdout.flush()
                sleep(0.007)
            print ()
            for x in text2:
                print (x, end='')
                sys.stdout.flush()
                sleep(0.007)
            print ()
            for x in range(text3):
                print (x, end='')
                sys.stdout.flush()
                sleep(0.007)
            for x in text4:
                print (x, end='')
                sys.stdout.flush()
                sleep(0.007)
            print()
            intro1()
    else:
        intro1() def end():
    text1 = 'done.'
    for x in text1:
        print (x, end='')
        sys.stdout.flush()
        sleep(0.007) intro()

我知道这看起来很长,很混乱。程序运行得很好,我已经测试过了。我唯一的问题是它会打印所有的数字。你知道吗

例如:

lives + 1 Thank you 0123 lives now.

或者

Thats rude - 1 live 01 lives left.

我只希望它打印最后一个数字,但我希望它打印的动画我有。我们将不胜感激。你知道吗


Tags: inimportforstdoutsyssleependprint
3条回答

我猜你不喜欢你的代码正在打印例如0123?您的代码有,例如:

   for x in range(text3):
        print (x, end='')
        sys.stdout.flush()
        sleep(0.007)

x in range(text3)所做的是先返回0,然后返回1,然后返回小于text3整数值1的连续数值。在python文档中查找内置range函数:https://docs.python.org/3/library/functions.html

相反,您可能只需要一行代码:

print( text3, end='' )

这将把text3转换成一个字符串并打印出来,你不会得到一个前导零:-)

还有你的代码有

    for x in text2:
        print (x, end='')
        sys.stdout.flush()
        sleep(0.007)

您所做的是一次打印一个字符的text2内容。这样写起来就简单多了:

print( text2, end='' )

如果下一次要调用print()开始新行,则可能不需要end=''。你知道吗

我不知道你为什么在那里有冲水和短暂的延迟-我不认为他们是必要的,从来没有见过使用过。啊,我猜你指的是动画。你是否考虑过通过定义一个函数来简化你的编码过程,这个函数可以进行动画打印,并在需要时简单地调用它?你知道吗

def aniprint( stringtoprint ):
    for c in stringtoprint:
        print( c,end='' )
        sys.stdout.flush()
        sleep(0.007)

那就叫它:

aniprint( text1 )
aniprint( text2 )   

HTH公司 巴尼

您应该更改:

for x in range(text3):
    print (x, end='')
    sys.stdout.flush()
    sleep(0.007)

for x in str(text3):
    print (x, end='')
    sys.stdout.flush()
    sleep(0.007)

注意区别,range(num)将返回一个数字列表,而str(num)将它转换为一个字符串,您可以从中一次打印一个字符。你知道吗

顺便说一句。。。遵循干燥(不要重复)的原则,你应该考虑把印刷品分成单一的功能

def type_output(text):
    for x in str(text):  # Use str to coerce the input to a string
        print (x, end='')
        sys.stdout.flush()
        sleep(0.007)

这样您的代码将变得更可读。你知道吗

当你打电话的时候

for x in range(text3):

当text3=2时,它将遍历“列表”[0,1],当text3=3时,它将遍历“列表”[0,1,2] 如果您只想要3个,请尝试:

for x in [3]: 

或者类似的东西,但是你可以简单的打印,也不需要迭代。你知道吗

相关问题 更多 >

    热门问题