¿Cómo inserta varias entradas al mismo tiempo en Python? [duplicar]

Sep 16 2020

Tenía poca experiencia con C ++ y existe esta función que realmente me gusta:

(este es un ejemplo simple para una calculadora con solo suma)

#include <iostream>
using namespace std;

int main(){
    int a, b;
    cout << "insert numbers and calculations" << endl;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}

Realmente me gusta el cinsistema ya que puedes tener múltiples entradas al mismo tiempo. Pero en Python, solo hago uno al mismo tiempo así:

float a = 0
float b = 0
a = input("insert fist number")
b = input("insert the second number")
print(a + b)

Quiero encontrar una manera de tener múltiples entradas al mismo tiempo similar a la cinsintaxis. ¿Cuáles son las soluciones a este problema?

Respuestas

2 quilliam Sep 16 2020 at 18:22

Puedes intentarlo de esta manera

a, b = [int(x) for x in input("Enter two values: ").split()] 
print("First Number is: ", a) 
print("Second Number is: ", b) 
print() 
1 Nikhil Sep 16 2020 at 18:25

Al usar split () puede ingresar varios valores al mismo tiempo.

a, b = input("Enter a two value: ").split() 
print("Value of a: ", a) 
print("Value of b: ", b)

Prueba esto

1 Yooooomi Sep 16 2020 at 18:20

Entrada

En cuanto a múltiples entradas a la vez, puede usar:

[input("Enter value {}: ".format(x + 1)) for x in range(2)]

Lo que crea una matriz de dos valores basados ​​en las dos entradas que se preguntarán de esta manera:

Enter value 1: YOUR_VALUE
Enter value 2: YOUR_VALUE

O si quieres algunos textos elegantes personalizados

texts = ["The first number: ", "And the second number: "]
x, y = [input(x) for x in texts]

O puede usar input().split()que se dividirá según los espacios

x, y = input("Two values: ").split() # Enter '10 10'

Fuente: https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/