0

I just saw this behavior in c ++:

cout << "\ nEnter two numbers:";
cin >> num >> num2;

In this way it is not necessary to do:

cout << "\ nEnter a number:";
cin >> num;

cout << "\ nEnter another number:";
cin >> num2;

So with that, the following question was generated:

How can I imitate this behavior in python3?

I've been trying to support myself with functions like range, but I still don't get the same behavior that happened in c ++

Does anyone know how I can achieve it?

Thank you

Carlos Bello
  • 262
  • 4
  • 10

1 Answers1

1

It's not that neat, but here's one way, using fileinput:

#!/usr/bin/python

import fileinput

print("Enter two numbers:")
count = 0
nums = []
for line in fileinput.input():
    nums.append(float(line.strip()))
    count += 1
    if count == 2:
        break

print(nums)
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32