0

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
user316108
  • 101
  • 1
  • You seem to be confusing the parameters permitted with *input()* with *print()* – DarkKnight Jan 08 '23 at 12:46
  • 1
    Does this answer your question? [How to print at the same line of an input?](https://stackoverflow.com/questions/70229694/how-to-print-at-the-same-line-of-an-input) – Gino Mempin Jan 08 '23 at 12:48

4 Answers4

0
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='')
Brixton
  • 11
  • 5
  • You were trying to use the `end` keyword argument, and the `input` function, which doesn't work, instead you can try using what I've recommended. – Brixton Jan 08 '23 at 12:48
  • 1
    This doesn't work. The tick or cross will be printed on the line below where the input occurred. Your second suggestion makes little sense as the question and answer are hard-coded – DarkKnight Jan 08 '23 at 13:01
  • Agh, I apologize for my mistake, you could try this. `answer = input('Capital of Japan: ', end=' ') if answer == 'Tokyo': print('✔', end='') else: print('❌', end='')` – Brixton Jan 08 '23 at 13:26
  • This should work, I've added `input` I'll edit my intial code for the fix. – Brixton Jan 08 '23 at 13:28
  • 1
    If you either test your code or re-read the question you will discover that *input()* does not accept the *end* keyword – DarkKnight Jan 08 '23 at 13:31
  • Well, excuse me sir, I have no idea in what way will he be using the code, so I'm giving out all the possibilities, so instead of criticizing, help. – Brixton Jan 08 '23 at 13:36
  • @Brixton It has nothing to do with criticizing, your answer is wrong. – Cow Jan 08 '23 at 13:45
  • 1
    The second code works but it prints "Capital of Japan: Tokyo ✔" in a new line, i'm looking for a way to print it on the same line – user316108 Jan 08 '23 at 13:50
  • 1
    Suggesting code that induces a TypeError is not a "possibility" – DarkKnight Jan 08 '23 at 13:56
  • User, then try this `answer = input('Capital of Japan: ') print('Capital of Japan: ', answer, ' ', end='', flush=True) if answer == 'Tokyo': print('✔', end='') else: print('❌', end='')` – Brixton Jan 08 '23 at 15:07
0

If your terminal supports ANSI CSI codes then:

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. n defaults to 1.

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

Example:

Capital of Japan: Tokyo ✔
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

you can accomplish your task with help of ANSI escape codes. 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. LINE_UP set the cursor to previous line and LINE_CLEAR clear the hole line for new text

-1

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:

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            
                  ✔