Home > database >  How to print a message in same line as a input? (python)
How to print a message in same line as a input? (python)

Time:01-08

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

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

CodePudding user response:

If your terminal supports ANSI CSI codes then:

CSI = '\x1B['
q = 'Capital of Japan: '
answer = input(q)
c = len(q)   len(answer)   1
result = '✔' if answer == 'Tokyo' else '❌'
print(f'{CSI}F{CSI}{c}C{result}')

CSI n F moves cursor to previous line. n defaults to 1.

CSI n C moves cursor forward n positions. n defaults to 1

CodePudding user response:

You can try both of these.

answer = input('Capital of Japan: ')
if answer == 'Tokyo':
    print('✔', end='')
else:
    print('❌', end='')

answer = input('Capital of Japan: ')
if answer == 'Tokyo':
    print('Capital of Japan: Tokyo ', end='✔')
else:
    print('Capital of Japan: Tokyo ', end='❌')
  • Related