hisoka Posted April 27, 2015 Share Posted April 27, 2015 In this web page : https://docs.python.org/2/tutorial/controlflow.html There is this construction : x = int(raw_input("Please enter an integer: ")) Why the person who wrote the page used int(raw_input(....) instead of only raw_input(....) ? they both give the same result so why complicate things ?? Link to comment Share on other sites More sharing options...
Ingolme Posted April 27, 2015 Share Posted April 27, 2015 The result of raw_input() is most likely a string, which means you can't operate mathematically with it. It needs to be cast to an integer first. Link to comment Share on other sites More sharing options...
hisoka Posted April 27, 2015 Author Share Posted April 27, 2015 "The result of raw_input() is most likely a string" it can be an integer too "which means you can't operate mathematically with it" I could as both raw_input() and int(raw_input()) , in our example , gave me the same result . So what do you say about it ? Link to comment Share on other sites More sharing options...
Ingolme Posted April 27, 2015 Share Posted April 27, 2015 The raw_input() function only returns a string, here's the documentation: https://docs.python.org/2/library/functions.html#raw_input If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. The question is what are you doing with result given by raw_input()? If you're just printing it, then you won't notice a difference. If you try to operate mathematically with it, there will be a difference. Casting to integer will prevent problems such as what would arise if the user gave a value such as "a" or "3N" 1 Link to comment Share on other sites More sharing options...
Jonathanks Posted April 27, 2015 Share Posted April 27, 2015 Hello. I'm don't know python and I'm still a beginner with PHP and JavaScript.The code above seems like a python implementation of the following in JavaScript:x = parseInt(prompt('enter a number', ''));Is this correct? Link to comment Share on other sites More sharing options...
hisoka Posted April 27, 2015 Author Share Posted April 27, 2015 now I used input() instead of raw_input() are they the same ? if I want to operate mathematically with input() too should I cast to an integer too like with raw_input() int(input("write a number:")) Link to comment Share on other sites More sharing options...
justsomeguy Posted April 27, 2015 Share Posted April 27, 2015 now I used input() instead of raw_input() are they the same ?Probably not, why would there be 2 functions that do the same thing?if I want to operate mathematically with input() tooshould I cast to an integer too like with raw_input()If you're going to treat it like an integer then cast it as an integer. 1 Link to comment Share on other sites More sharing options...
Ingolme Posted April 27, 2015 Share Posted April 27, 2015 You should read the documentation. Here's the description of input(): https://docs.python.org/2/library/functions.html#input Equivalent to eval(raw_input(prompt)). In other words: The user's input is evaluated as Python code. I would not recommend using this. Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 (edited) "In other words: The user's input is evaluated as Python code" sorry I did not ,exactly, understand your meaning . Could you , please , explain it more clearly ? Here http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x it is stated that using input() is better than raw_input() raw_input() takes exactly what the user typed and passes it back as a string. input() takes the raw_input() and performs an eval() on it as well. The main difference is that input() expects a syntactically correct python statement where raw_input() does not. in matter of making errors input() is more strict than raw_input() it is mentioned too that : raw_input() was renamed to input() and the old input() was removed. If you want to use the old input(), you can do eval(input()). may be this is why when I use raw_input() instead of input() python interpreter gives me this error: NamError : name 'raw_input' is not defined What do you think ? Edited April 30, 2015 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 30, 2015 Share Posted April 30, 2015 When I say that the user's input is evaluated as Python code I mean that the user can write python code into the input and your program will run that code. The different between raw_input() and input() is clear in the page you just linked to, what don't you understand? It seems raw_input() does not exist in versions of Python 3 and above, which means that you do need to use input(). You still need to cast it as an integer if you want to do integer operations with it. 1 Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 FoxyMod good morning Thank you for your reply I understand more better now for example : >>> s = input( 400+458+78) 936 not only the input() function outputs what is inside its parenthesis but it evaluates it and give the result Link to comment Share on other sites More sharing options...
Ingolme Posted April 30, 2015 Share Posted April 30, 2015 No, any function call will evaluate expressions before passing them to the function. In Python 2, writing input() was the same as writing eval(input()) in Python 3. I assume your version of Python is version 3. What happens is that if you write s = eval(input()) and the user writes abs(-5), instead of getting the string "abs(-5)" you would get "5" Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 "No, any function call will evaluate expressions before passing them to the function" Could you tell me which part in python is responsible for evaluating an expression before passing them to any function? I mean its name . What is its name ? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2015 Share Posted April 30, 2015 You ask some strange questions. I suppose that would be the interpreter. It is the part that actually executes the code. 1 Link to comment Share on other sites More sharing options...
hisoka Posted May 3, 2015 Author Share Posted May 3, 2015 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now