0

I am making a Python program for a school project and I want to say: 'If input is <enter> break'. I tried to write:

a = input('>')

if a == '\n':
    break

and it didn't work. How do I fix this?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
angeloskar
  • 19
  • 1
  • 3
    `input()` does not include the newline character that ended the user's typing. To detect a blank line, you'd use `if a == '':` (or simply `if not a:`). – jasonharper Nov 24 '21 at 18:56
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – RoseGod Nov 24 '21 at 19:02
  • "If input is break" What does that *mean*? Anyway, here's a quick way that you can figure out how to write this code: type the interesting input at the prompt, and then *see what the value of `a` is*. – Karl Knechtel Nov 24 '21 at 19:06
  • @KarlKnechtel Formatting issue... See edit – Tomerikoo Dec 06 '21 at 12:13

2 Answers2

0

When a user presses the enter key, the input taken is a blank string. so you want your code to say:

if a == "":
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Nomzz
  • 23
  • 5
0

This is your desired code. It breaks the loop when you only press the enter button as input:

while True:
    a= input()
    if a =='':
        break
    else: continue
Nimantha
  • 6,405
  • 6
  • 28
  • 69