繁体   English   中英

Python正在打印所有内容,我只希望它打印最后一个数字

[英]Python is printing everything, I only want it to print the last number

到目前为止,这是我的代码:

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

我知道这看起来很长,而且令人困惑。 该程序运行良好,我已经对其进行了测试。 我唯一的问题是它将打印所有数字。

例如:

生活+ 1谢谢0123现在生活。

要么

那很粗鲁-还剩1个直播01个。

我只希望它打印最后一个数字,但我希望它与我拥有的动画一起打印。 帮助将不胜感激。

您应该更改:

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)会将其转换为一个字符串,您可以一次从该字符串打印一个字符。

顺便说一句...遵循DRY(不要重复自己)的原则,您应该考虑将打印内容分解为一个功能,

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)

这样,您的代码将更具可读性。

range(text3)更改为str(text3)

range(text3)返回列表[0,1,2...text3-1] 因此,当您要遍历数字的字符串表示形式中的字符时,for循环遍历该整数列表。 要解决此问题,只需将整数text3更改为字符串,然后以与遍历其他字符串相同的方式遍历字符。

我猜您不喜欢您的代码正在打印,例如0123? 您的代码所在的位置,例如:

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

x in range(text3)正在执行的操作是先返回0,然后返回1,然后返回比text3的整数值小1的连续数值。 在此处的python文档中查找内置范围函数: 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 Barny

你打电话时

for x in range(text3):

当text3 = 2时,它将遍历“列表” [0,1],当text3 = 3时将迭代[0,1,2]。如果只需要3,则尝试:

for x in [3]: 

或类似的东西,但是您也可以简单地进行打印而无需迭代。

暂无
暂无

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

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