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.
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.
Use input(), map() and split() function to take space-separated integer input in Python 3.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With