[Tutor] two input acceptions
finalyugi at sapo.pt
Sat May 19 02:47:51 CEST 2007
adam urbas escreveu:
> Thanks for the help. I've made quite some progress since I first posted this email. I have a question though, what did you mean when you were talking about the raw_input( )? How can the regular input( ) be used evilly? If you could explain in depth, I would be very grateful. I have a new question related to my program area.py., I guess it's the same one as before. When I run the program and input the rectangle option, it asks me for a radius, unless I input 1, instead of rectangle. How do I program it to accept both 1 and rectangle?> Date: Sat, 12 May 2007 18:55:20 +0100> From: finalyugi at sapo.pt> To: adamurbas at hotmail.com> CC: tutor at python.org> Subject: Re: [Tutor] (no subject)> > adam urbas escreveu:> > Hi,I just started python today and I would like a few pointers, if you don't mind. I tried using a tutorial, but was only able to get the correct results for the most basic problems. # Area calculation programprint “Welcome to the Area calculation program”print “––
–––––––––––”print# Print out the menu:print “Please select a shape:”print “1 Rectangle”print “2 Circle”# Get the user’s choice:shape = input(“> “)# Calculate the area:if shape == 1: height = input(“Please enter the height: “) width = input(“Please enter the width: “) area = height*width print “The area is”, areaelse: radius = input(“Please enter the radius: “) area = 3.14*(radius**2) print “The area is”, areaI've been trying to get this to work. I was on a forum on Google and they said to put:input("press ENTER to continue")at the end. I did, but it didn't work. It runs the program but just shuts itself off when its done and i don't even get to select any of the option things that i'm s> upposed to be able to select. It just turns on then back off and I don't even get to see anything. Could someone help me out.ThanksAdam> > _________________________________________________________________> > Create the ultimate e-mail address book. Import your cont
acts to Windows Live Hotmail.> > www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507> > > > > > ------------------------------------------------------------------------> > > > _______________________________________________> > Tutor maillist - Tutor at python.org> > http://mail.python.org/mailman/listinfo/tutor> > First, welcome to the world of Python. :D> Second. please give a title when you start a new thread on a mailing list.> Third, format your posts and code. Since Python uses indented code, it's > kinda hard to read it when it's all in one line (Don't worry, I'll paste > it indented in a file attached to this email :D )> > Now for the code.> > After arranging the code, the first thing I noticed were this characters “ ”> > I tried running the code, and if gave me a error there, so I just > replace then with " ", and voilá, the code worked :D . So the lesson > here is always use either " " or ' ' in the code.> > Oh, a
lso another thing. Don't use input() to get the user input, because > that command can run code and it may be evilly used. Always use > raw_input() instead :D .> > Anyway, I hope I helped you,> > > -- > _> ASCII ribbon campaign ( )> - against HTML email X> & vCards / \
> Create the ultimate e-mail address book. Import your contacts to Windows Live Hotmail.
First of all, what email client are you using?
Because the text is getting all weird and difficult to read (it's all in
one line, with no paragraphs and things like that).
Now, the thing about input() and why it's not a good policy to use is
that, unlike raw_input(), what type in a input() is executed by Python
(in raw_input() is stored as a string).
var = raw_input()
Now we have a variable called var which contains the string that says
You can confirm that by typing:
There, no harm done. Now let's try the same thing using the input() command:
var = input()
Now let's type "print var" again as we did before.
>> ['L', 'O'. 'L']
Now what happened? Because you used the input() command, what you type
was interpreted by Python, instead of being stored in a string and since
the list() command is used to create a list, Python did just that. He
created a list. Now, in this example, no harm was done. But image
someone typing the command os.system("command to delete some file or run
some file"). That would send a delete command to the terminal, or
install some file (it could even be a virus).
Ok, it's a little harder to explain, but the thing you should is that
usually raw_input() = GOOD, input() = BAD.
Now, I couldn't quite understand the second problem.
Please explain a little better.
PS: Now I know why I see all posts messed up. It's because you're
sending your emails as a HTML, and I deactivated that on my email
client. I don't know if Hotmail (I believe you send you emails from
there) as an option to turn off HTML. If it was please use it :D
(Besides being nice, you can get more responses if you do that. Not
everyone has an HTML capable email client.)
PS2 (no, not the console): I just noticed you didn't send the email back
to the mailing list. You should select reply to all (or a similar
option) when replying to mailing list, so that other people can learn too.
ASCII ribbon campaign ( )
- against HTML email X
& vCards / \
More information about the Tutor