删除已经用 Python 打印的 [英] Deleting already printed in Python

查看:60
本文介绍了删除已经用 Python 打印的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了练习,我正在尝试用 Python 做一些事情.我决定制作一个简单的刽子手游戏 - 我不是在制作 GUI.游戏将从一个简单的 input() 开始.现在,除了要求输入之外,我希望下一行删除隐藏的单词.我试过使用 \b (退格字符),但它不起作用.类似的东西:

For practice, I'm trying to do some stuff in Python. I've decided to make a simple hangman game - I'm not making a GUI. The game would start with a simple input(). Now, I'd like next line to, beside asking for input, to delete the hidden word. I've tried using \b (backspace character), but it's not working. Something like:

word = input("Your word: ")
for i in range(len(word) + 12):
    print("\b")

现在,打印反冲字符应该删除输入和你的话",但它没有做任何事情.如果我在 IDLE 中执行此操作,我会得到正方形,如果我通过单击打开它,则什么也得不到.

Now, printing the backlash character is supposed to delete the input and "Your word", but it isn't doing anything. If I do this in IDLE I get squares, and I get nothing if I open it by clicking.

如何实现?恐怕我对我的问题不太清楚,但我希望你能明白我的意思.:)

How to accomplish this? I'm afraid I wasn't too clear with my question, but I hope you'll see what I meant. :)

推荐答案

我假设输入单词的玩家希望确保他们输入的内容正确,所以您可能希望在他们输入时显示该单词,对吗?

I assume the player entering the word wants to be sure they've entered it correctly so you probably want to display the word as they're typing it right?

如何打印足够多的 \n 以在完成后将其移出屏幕或发出清除屏幕命令?

How about printing enough \ns to move it off the screen when they're done or issue a clear screen command?

你提到这是一个简单的游戏,所以一个简单的解决方案似乎很合适.

You mentioned this was a simple game so a simple solution seems fitting.

这是一个简单的例程,可以在几乎任何平台上清除控制台(取自 此处):

Here's a simple routine to clear the console on just about any platform (taken from here):

def clearscreen(numlines=100):
    """Clear the console.
    numlines is an optional argument used only as a fall-back.
    """
    import os
    if os.name == "posix":
        # Unix/Linux/MacOS/BSD/etc
        os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
        # DOS/Windows
        os.system('CLS')
    else:
        # Fallback for other operating systems.
        print '\n' * numlines

这篇关于删除已经用 Python 打印的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆