Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking inputs in python

Recently I started studying about Python and I have come across one problem. Suppose you are given 3 space separated integers, say 4 5 6
When I use input() method and take the input, it is showing me an error.

Traceback (most recent call last):
  File "P_Try.py", line 1, in <module>
    x= input();
  File "<string>", line 1
    4 5 6 
      ^
SyntaxError: invalid syntax

I guess, since it is in one line, it is assuming it to be a string, but finds out the integer at the location 2 (index starting from 0). I tried alternative method that I took the input as a string using raw_input() method and and wherever I find a number, I cast it as int and append it to the list.

Is there any better way of accomplishing the task?

like image 777
codeGeek_2015 Avatar asked Apr 18 '26 02:04

codeGeek_2015


1 Answers

Function input() is interpreting your input as a Python code, I know, it's little odd. To get raw input (string containing user typed characters), just use raw_input() function instead.

like image 81
Tupteq Avatar answered Apr 20 '26 15:04

Tupteq