0

Example of what I want/have:

print(input('> Discord Webhook: '), f'{"None" if len(input) < 1 else input}')

I want it to print None if the input value is less than 1, or else just print the output normally.

vlone
  • 33
  • 7
  • I don't think you can do that because `input` is a built-in function that is called everytime it is in the code. You can just do `webhook = input('> Discord Webhook: ')` and then do anything with the string as you like. – Minh-Long Luu Dec 13 '22 at 15:12
  • @Minh-LongLuu I have tried printing it with `end=""` after the input but it still prints on new line. – vlone Dec 13 '22 at 15:13

3 Answers3

0

If you want just to print None in place of an empty string you can do that with a simple or:

print(input('> Discord Webhook: ') or None)

Note that this does not actually capture the input for you to use on a later line. Usually when you call input() you want to assign its result to a variable:

webhook = input('> Discord Webhook: ') or None
print(webhook)
# do whatever else you're going to do with that value
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • The input will still be shown in the console though :/ Showing it twice doesn't make sense. – vlone Dec 13 '22 at 15:18
  • It doesn't, but that's what OP asked how to do... ¯\\_(ツ)_/¯ – Samwise Dec 13 '22 at 15:37
  • I am the OP and I want the output on the same line as the input to make sense for the user. – vlone Dec 13 '22 at 15:40
  • That wasn't mentioned in the question at all. Sounds like what you're really after is https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line – Samwise Dec 13 '22 at 15:50
0

I was able to accomplish this by making it appear like the input was changed to none using the clear command and printing the input text with "None" after it.

import os
webhook = input('> Discord Webhook: ')
if len(webhook) < 1:
  os.system('clear')
  print('> Discord Webhook: None')
vlone
  • 33
  • 7
0

If you want to change what shows up in terminal where input was given, you can use ANSI escape codes to control the cursor directly. This way you can edit specific parts of the terminal output after they have already been printed.

prompt = '> Discord Webhook: '
webhook = input(prompt)
if not webhook:
    print('\u001b[1A', end = "") # Move cursor one line up
    print(f'\u001b[{len(prompt)}C', end = "") # Move cursor past prompt
    print('\u001b[0K', end = "") # Clear rest of line
    print("None")

On Windows you might need to do the following for cmd to recognise ANSI codes properly:

import os
os.system("")
matszwecja
  • 6,357
  • 2
  • 10
  • 17