0
How to get input to work in python?
I tried doing a simple Python project on my iPad that required text input - but when I ran the code and it asked me for the input, it just showed an error message (NameError: name '...' is not defined) when it was entered. I don't know if it is a fault in the program or just the interpreter which I'm using, but can some please give me some suggestions about what might be happening. P.S. I tried doing the simple calculator program from the course this is the non-working part of the program: choice = input();
5 Answers
+ 8
It would be a good idea to post the code you written for community inspection and help.
+ 4
Show us the code you wrote: it should be a mistake in it ^^... maybe have you fogotten to enclose the input() parameter inside quotes, for example?
+ 4
@Kash:
Use of 'eval' is unadviced, as it's considareted as unsafe... better practice is to avoid it and rather use int() or float() to cast the input() returned string value:
int_num = int(input("enter an integer? ")
float_num = float(input("enter an integer? ")
You can even write a custom function to cast a string to the best suited numeric type:
def number(s):
if '.' in s:
return float(s)
else return int(s)
Obviously, this can be improved with error handling ^^
+ 1
If you are willing to take the numerical input you gotta use "eval" . Input returns the string in Python . In any case you gotta upload your code
- 1
what sites would be best? would StackOverflow be good?