1

I had little experience with C++ and there is this function which I really like about it:

(this is a simple example for a calculator with only addition)

#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;
}

I really like the cin system since you can have multiple inputs at the same time. But on Python, I only one at the same time like this:

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

I want to find a way to have multiple inputs at the same time similar to the cin syntax. What are the solutions to this problem?

KripC2160
  • 49
  • 1
  • 8

3 Answers3

1

Input

As for multiple inputs at once, you can use:

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

Which creates an array of two values base on the two inputs which will be asked this way:

Enter value 1: YOUR_VALUE
Enter value 2: YOUR_VALUE

Or if you want some fancy customized texts

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

Or you can use input().split() that will split based on spaces

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

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

Yooooomi
  • 895
  • 1
  • 8
  • 20
1

You may try this way

a, b = [int(x) for x in input("Enter two values: ").split()] 
print("First Number is: ", a) 
print("Second Number is: ", b) 
print() 
quilliam
  • 182
  • 1
  • 9
1

By using split() you can input multiple values at the same time.

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

Try this

Nikhil
  • 11
  • 5