Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read two inputs separated by space in a single line?

Tags:

I want to read two input values. First value should be an integer and the second value should be a float.

I saw Read two variables in a single line with Python, but it applies only if both the values are of same type. Do I have any other way?

Example input, first is int and second is float. The inputs should be on a single line:

20 150.50 

http://www.codechef.com/problems/HS08TEST/

I'm very new to Python.

like image 518
Prince Ashitaka Avatar asked Nov 12 '10 08:11

Prince Ashitaka


People also ask

How do you take space separated inputs?

There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each value. Using nextInt( ) method of Scanner class.

How do you take two inputs separated by space in Python?

Use input(), map() and split() function to take space-separated integer input in Python 3.

How do you read two space separated integers in Python?

Thanks. Easy solution is just to use the string method split. input: 5 8 0 sgshsu 123 input. split(" ") == ["5", "8", "0", "sgshsu", "123"] #Then they are easy to convert to numeric datatypes, but invalid inputs might raise errors.


1 Answers

Like this:

In [20]: a,b = raw_input().split() 12 12.2  In [21]: a = int(a) Out[21]: 12  In [22]: b = float(b) Out[22]: 12.2 

You can't do this in a one-liner (or at least not without some super duper extra hackz0r skills -- or semicolons), but python is not made for one-liners.

like image 190
Gabi Purcaru Avatar answered Sep 20 '22 13:09

Gabi Purcaru