Hi, again, I am very new to PyDev, I started yesterday.
I have a Python book, but I cannot find in it how to ask for input. For instance, in java, you can import kb.Scanner and do something like this:
System.out.print("Enter your input: ");
int inpt = kb.nextInt();
And then inpt becomes what you entered. Can somebody tell me how to do this in Python?
Thank you!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
One more question though. It seems like using sys.stdin reads the input as a string. is it possible to change it so that it reads integers, lists, dictionaries, etc?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No, it only reads Strings.
You can, however, use casts like int() to automatically transform the input.
if you have a properly formatted List or Dictionary (properly as in "like the print statement prints it"), you can use
eval(THING_TO_TRANSFORM), {"__builtins__":[]}, {}) (the second parameters are there fore security purposes, to disable the builtin commands in this evaluation statement).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, again, I am very new to PyDev, I started yesterday.
I have a Python book, but I cannot find in it how to ask for input. For instance, in java, you can import kb.Scanner and do something like this:
System.out.print("Enter your input: ");
int inpt = kb.nextInt();
And then inpt becomes what you entered. Can somebody tell me how to do this in Python?
Thank you!
I recommend http://diveintopython.org/
For reading the user input, try reading from sys.stdin - see the above book for an example
hope that helps.
Thank you, now I can input things!
One more question though. It seems like using sys.stdin reads the input as a string. is it possible to change it so that it reads integers, lists, dictionaries, etc?
No, it only reads Strings.
You can, however, use casts like int() to automatically transform the input.
if you have a properly formatted List or Dictionary (properly as in "like the print statement prints it"), you can use
eval(THING_TO_TRANSFORM), {"__builtins__":[]}, {}) (the second parameters are there fore security purposes, to disable the builtin commands in this evaluation statement).
Actually, there's a function called 'input()' which will get the text entered and make an eval on the string entered and may return ints, lists, etc. See http://en.wikibooks.org/wiki/Python_Programming/Input_and_output for a reference.
Cheers,
Fabio
Sorry, that should have read eval(THING_TO_TRANSFORM, {"__builtins__":[]}, {}) - there was a surplus bracket