Jump to content

explanation about a python construction


hisoka

Recommended Posts

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

"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

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"

  • Like 1
Link to comment
Share on other sites

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.
  • Like 1
Link to comment
Share on other sites

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

"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 by hisoka
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

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

"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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...