[英]How to print a message in same line as a input? (python)
When the user confirms the input i want to print a message in the same line of the input, in this case, a symbol of correct or incorrect.当用户确认输入时,我想在输入的同一行打印一条消息,在这种情况下,是正确或不正确的符号。
I tried using end='', but it doesn't seem to work我尝试使用 end='',但它似乎不起作用
answer = input('Capital of Japan: ', end=' ')
if answer == 'Tokyo':
print('✔')
else:
print('❌')
So if the user type 'Tokyo', that is what i expected to show up:因此,如果用户键入“东京”,这就是我希望显示的内容:
Capital of Japan: Tokyo ✔
Instead, i get this error:相反,我收到此错误:
answer = input('Capital of Japan: ', end=' ')
TypeError: input() takes no keyword arguments
If your terminal supports ANSI CSI codes then:如果您的终端支持 ANSI CSI 代码,则:
CSI = '\x1B['
q = 'Capital of Japan: '
answer = input(q)
c = len(q) + len(answer)
result = '✔' if answer == 'Tokyo' else '❌'
print(f'{CSI}F{CSI}{c}C {result}')
CSI n F moves cursor to previous line. CSI n F 将 cursor 移动到上一行。 n defaults to 1.
n 默认为 1。
CSI n C moves cursor forward n positions. CSI n C 将 cursor 向前移动 n 个位置。 n defaults to 1
n 默认为 1
Example:例子:
Capital of Japan: Tokyo ✔
you can accomplish your task with help of ANSI escape codes.您可以借助 ANSI 转义码完成您的任务。 use this:
用这个:
import colorama as cr
cr.init(autoreset=True)
LINE_UP = "\033[1A"
LINE_CLEAR = "\x1b[2K"
Question = "Capital of Japan: "
answer = input(Question)
if answer == "Tokyo":
print(LINE_UP, LINE_CLEAR, Question + answer +
f"{cr.Fore.GREEN} ✔")
else:
print(LINE_UP, LINE_CLEAR, Question + answer +
f"{cr.Fore.RED} ❌")
with help of colorama you can print colorful text in terminal.在 colorama 的帮助下,您可以在终端中打印彩色文本。 LINE_UP set the cursor to previous line and LINE_CLEAR clear the hole line for new text
LINE_UP 将 cursor 设置为上一行,LINE_CLEAR 清除新文本的空洞线
answer = input('Capital of Japan: ')
print('Capital of Japan: ', answer, ' ', end='', flush=True)
if answer == 'Tokyo':
print('✔', end='')
else:
print('❌', end='')
or或者
answer = input('Capital of Japan: ', end=' ')
if answer == 'Tokyo':
print('✔', end='')
else:
print('❌', end='')
I think it really can't be done.我觉得真的做不到。 Try it with
\b
which will position the cursor on the last line of the cmd. I tried it this way:在 cmd 的最后一行用
\b
试试 position cursor。我这样试过:
answer = input('Capital of Japan: ')
if answer == 'Tokyo':print(f'\b\bCapital of Japan: {answer} ✔', end=' ')
else:print(f'\b\bCapital of Japan: {answer} ❌', end=' ')
But I haven't gotten it to work.但我还没有让它工作。 One solution I've come up with is to print another line below, and find some spacing for the icons/emogi to be positioned below the response.
我想出的一个解决方案是在下面打印另一行,并为图标/表情符号找到一些间距以放置在响应下方。 The code is like this.
代码是这样的。
answer = input(print('Capital of Japan: ', end=' '))
if answer == 'Tokyo':print('\b' + ' ' * len('Capital of Japan: ') + '✔', end=' ')
else:print('\b' + ' ' * len('Capital of Japan: ') + '❌', end=' ')
Gives a result something like this:给出这样的结果:
Capital of Japan: example
❌
Capital of Japan: Tokyo
✔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.