April 3rd, 2005, 11:00 PM
need raw_input and protect bad input help
i am a beginning programmer and i want to know the difference between input and raw_input is. by the looks of it before raw_input is used to protect from bad input but what exactly does it do?
also how can i protect from bad input in a text entry field in tkinter? for example in my text entry field it asks you to input only 1 letter and it can only be a letter. i was thinking of using an if statement. like
i was thinking something like that but i saw someone use a "try" statement before. is that a better way and how do you use it?
if x in char: #x is letter and char is list of alphabet
April 4th, 2005, 02:40 AM
I can't help you with the tkinter stuff, but the raw_input function allows you to accept user input; eg:
words=raw_input("Type some stuff")
On the kind of input you expect the user to enter, you can convert the data by putting str, int, etc. in front of the raw_input statement. What I like to do is take the raw_input given, copy it, and convert it to the type that works for the application, that way you have both to use, which is handy, depending on how you have to manipulate the user input. But again, that depends on what else is in your code.
Hope this helps
April 4th, 2005, 02:51 AM
...and more fun
I don't know much about tkinter modules and functions, but for python, I would write the line something like:
of course you could also do it as just x.isalpha()==True, with contingencies on the len of x, but you get the idea.
if x.isalpha()==True and len(x)==1:
if x.isalpha()==True and len(x)!=1:
April 4th, 2005, 03:56 AM
in terms of taking input from the console, you should always use raw_input() rather than input(), as input() uses eval to convert the input to an integer which a mean person could use to their advantage if you want to get input in the form of an int that you can work with mathematically, use int(raw_input("Int: "))
April 4th, 2005, 07:33 AM
As rabbit mentioned input() is a nice security risk; one that should have never found its way into the language if you ask me . Given a prompt that uses input it's surprisingly easy to execute commands on the computer in question i.e.
Note: this works because calling input() is the same as doing eval(raw_input()). Using eval is less dangerous than exec but you still need to be very careful where you use it .
Mark-Smiths-Computer:~ Mark$ ls
Desktop Downloads Movies Pictures Public
Documents Library Music Projects Sites
Mark-Smiths-Computer:~ Mark$ python2.4
Python 2.4 (#1, Dec 12 2004, 22:12:51)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Enter a Number: ') #Friendly user...
Enter a Number: 20
>>> input('Enter a Number: ') #Not so friendly...
Enter a Number: __import__('os').popen('ls').read()
Everyone has to convert user input to a desired type at some point – often many times in a program! If so you should write a function to handle the details for you and call it whenever you need to get input of that type . Here's an example of this:
As for your problem I would do something like this (below). This simply says if the length of the users input (x) is 1 then we're good. Otherwise we have a little problem.
def castInput(prompt, cast):
#Tries to cast the users input to the desired type. If this fails
#(raises a ValueError) the function is called recursively until a
#valid value is given.
if __name__ == '__main__':
castInput('Enter a string: ', str) #Enter any value here.
castInput('Enter an int: ', int)
castInput('Eneter a float: ', float)
Again I don't know much about TKinter sorry so I can't really help much there sorry.
if len(x) == 1:
Hope this helps,
April 4th, 2005, 08:28 AM
I think there is no problem with "bad input" in an Entry in Tkinter, because entry.get() always returns a string.
To check the input or restrict the input length, have a look at Fredrik Lundh's validating entry widget.
April 4th, 2005, 08:52 PM
Any true expression evaluates to True. It's also good to not evaluate the same thing multiple times. You thus only need:
Originally Posted by burninator
April 5th, 2005, 12:09 PM
or how about you do something like this.
This code does not work directly something is wrong i still have to learn it again.
from Tkinter import *
root = Tk()
char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for x in text.get():
if x in char:
text = StringVar(root)
textx = StringVar(root)
But I would think something like that
April 5th, 2005, 02:44 PM
from Tkinter import *
max_len = 1
text = e.get()
if text.isalpha() and len(text)<=max_len:
"Good job","Yes, <%s> is a valid input." % text)
# and do something else
root = Tk()
e = Entry()