How to kill a while loop True with the enter key

0

How can I end a while while True cycle using a line break? I have this little function:

def number_add():
    print("3[1;33m"+"Please enter two numbers: \n" + "3[;36m"+ "Operator 1: ", end='')
    a = input()
    print("3[;36m"+"Operator 2: ",end='')
    b = input()
    print("3[4;35m"+"Result: %s + %s = %s" %(a, b, str(int(a) + int(b))))
    while True:
        if input() == '\n':
            break
    clear()

When I try to end it with the enter key it does not work, it only makes a line break in the console, what is the error?

    
asked by Kevin Gutierrez 21.11.2018 в 05:13
source

5 answers

2

To work with the keyboard you must have the keyboard library. I attached a simple example.

import keyboard

while True:

    print('Me imprimo infinitamente hasta que presionen la tecla esc')

    if keyboard.is_pressed('esc'):
        break

As Abulafia says to install it, you should use pip :

pip install keyboard

I've attached the documentation .

Greetings!

    
answered by 21.11.2018 в 10:24
0

instead of directly using a True for the while it would be good if you use a Boolean variable with a True value, and when you press the key you change the variable to False. With respect to the key that presses it would be good to use events for the keyboard.

    
answered by 21.11.2018 в 05:22
0

I do not know what you mean. If you want to wait for an enter to continue, you do not need a while.

null_input = input()
doSomething()
    
answered by 22.11.2018 в 23:13
0

Since input is a string (by default) it simply evaluates that it is not null, if it is null it is because that string does not exist, if you place '/n' you are evaluating a line break instead of as a empty , if what you want is that when you type Enter it comes out, I evaluate it like this:

a=input()
while True:
    print(a)
    if a=='':
        break
    a=input()

Also if you want the code to continue through the cycle evaluating while only the variable a is different from empty, you can leave it like this:

def number_add():
    a = input("3[1;33m"+"Please enter two numbers: \n" + "3[;36m"+ "Operator 1: ")
    while True:
        if a=='':
            break
        b = input("3[;36m"+"Operator 2: ")
        print("3[4;35m"+"Result: %s + %s = %s" %(a, b, str(int(a) + int(b))))
        a = input("3[1;33m"+"Please enter two numbers: \n" + "3[;36m"+ "Operator 1: ")

number_add()

The moment a is empty, the cycle is broken and no more is evaluated.

    
answered by 25.11.2018 в 15:57
-2

@Kevin is right. And I think you can refer to something like this.

def number_add():
print("3[1;33m"+"Please enter two numbers: \n" + "3[;36m"+ "Operator 1: ", end='')
a = input()
print("3[;36m"+"Operator 2: ",end='')
b = input()
print("3[4;35m"+"Result: %s + %s = %s" %(a, b, str(int(a) + int(b))))
c = input() //no se si el intro puede recogerse así
d = True
while d:
    if c == '\n': //tampoco estoy seguro de si esto es correcto
       d = False
clear()

I do not know if the syntax is exactly like that, I from python have not seen much.

Greetings!

    
answered by 21.11.2018 в 09:02