<html>
<head>
<style>
P
{
margin:0px;
padding:0px
}
body
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body><div style="text-align: left;">Thank you for the help Brian. I would like to ask you about these things. Which one of the examples you gave would be most fool proof.<br></div><br><br><br><hr id="stopSpelling">> Date: Wed, 23 May 2007 13:40:09 -0400<br>> From: broek@cc.umanitoba.ca<br>> To: adamurbas@hotmail.com<br>> CC: tutor@python.org<br>> Subject: Re: [Tutor] trouble with "if"<br>> <br>> adam urbas said unto the world upon 05/23/2007 01:04 PM:<br>> > Sorry, I don't think Hotmail has turn off HTML. If it does I<br>> > havn't been able to find it. I think you're going to have to<br>> > explain your little bit of text stuff down there at the bottom. I<br>> > have no idea what most of that means. All my choice things are<br>> > working now though. I think that is what you were trying to help<br>> > me with. What I used wasif shape in["1","circle"]:and if shape ==<br>> > "1" or shape =="circle":It works perfectly fine now.Ya that little<br>> > bit o' code is really puzzling. I wish I knew more about this<br>> > python deal. I understand the concept, but not the rules or the<br>> > techniques and things of that sort. OK... I've got it... the<br>> > data=raw_input('Feed Me!'). Ok I now understand that bit. Then it<br>> > says Feed Me! and you put 42 (the ultimate answer to life the<br>> > universe, everything). OK, it won't accept the <type 'str'> bit.<br>> > it doesn't like the "<". Well, I just removed that bit and it<br>> > said:Feed Me! and I put 42, and it said >>> (I guess it's<br>> > satisfied now, with the whole feeding). Well if I understood what<br>> > 'str' meant, then I could probably figure the rest out. Well I<br>> > have to go do other things so I'll save the rest of this figuring<br>> > out till later.I shall return,Adam> Date: Wed, 23 May 2007 12:12:16<br>> > -0400> From: broek@cc.umanitoba.ca> To: adamurbas@hotmail.com> CC:<br>> > tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam<br>> > urbas said unto the world upon 05/23/2007 11:57 AM:> > > > Hi all,><br>> > > > > I've been working with this new program that I wrote. I<br>> > started out > > with it on a Ti-83, which is much easier to program<br>> > than python. Now > > I'm trying to transfer the program to python<br>> > but its proving to be quite > > difficult. I'm not sure what the<br>> > whole indentation thing is for. And > > now I'm having trouble<br>> > with the if statement things. > > > > #"Circle Data Calculation<br>> > Program:"> > print "Welcome to the Circle Data Calcuation<br>> > Program."> > print> > > > #"Menu 1:"> > print "Pick a shape:"><br>> > > print "(NOTE: You must select the number of the shape and not the<br>> > shape > > itself)"> > print "1 Circle"> > print "2 Square"> > print<br>> > "3 Triangle"> > > > #"User's Choice:"> > shape=raw_input("> ")><br>> > > > > #"Select Given:"> > if shape == 1:> > print<br>> > "Choose the given value:"> > print "1 radius"> ><br>> > print "2 diameter"> > print "3 circumference"> ><br>> > print "4 area"> > > > #"User's Choice:"> > given=raw_input("> ")> ><br>> > > > if given == 1:> > radius=raw_input("Enter Radius:")> ><br>> > diameter=(radius*2)> > circumference=(diameter*3.14)> ><br>> > area=(radius**2*3.14)> > print "Diameter:", diameter> ><br>> > print "Circumference:", circumference> > print "Area:",<br>> > area> > > > if given == 2:> > diameter=raw_input("Enter<br>> > Diameter:")> > radius=(diameter/2)> ><br>> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> ><br>> > print "Radius:", radius> > print "Circumference:",<br>> > circumference> > print "Area:", area> > > > if given == 3:><br>> > > circumference=raw_input("Enter Circumference:")> ><br>> > radius=(circumference/3.14/2)> > diameter=(radius*2)> ><br>> > area=(radius**2*3.14)> > print "Radius:", radius> ><br>> > print "Diameter:", diameter> > print "Area:", area> > > ><br>> > if given == 4:> > area=raw_input("Enter Area:")> ><br>> > radius=(area/3.14)> > > > This is the whole program so<br>> > far, because I haven't quite finished it > > yet. But I tried to<br>> > get it to display another list of options after you > > select a<br>> > shape but it just does this.> > > > Pick a shape:> > 1 Circle> > 2<br>> > Square> > 3 Triangle> > >1> > >1> > >>>> > > > I'm not sure why<br>> > it does that but I do know that it is skipping the > > second list<br>> > of options.> > > > Another of my problems is that I can't figure<br>> > out how to get it to > > accept two different inputs for a<br>> > selection. Like I want it to accept > > both the number 1 and<br>> > circle as circle then list the options for > > circle. It won't<br>> > even accept words. I can only get it to accept > > numbers. It's<br>> > quite frustrating actually.> > > > Any advice would be greatly<br>> > appreciated.> > Thanks in advance,> > Adam> > > > > > > Adam,> ><br>> > Could you send plain text email rather than html, please? At least<br>> > for > me, your code's indentation is all messed up unless I take<br>> > some steps > to rectify it.> > The problem is that raw_input<br>> > returns a string, and you are testing > whether given is equal to<br>> > integers. See if this helps make things clear:> > >>> data =<br>> > raw_input('Feed me!')> Feed me!42> >>> type(data)> <type 'str'>><br>> > >>> data == 42> False> >>> int(data) == 42> True> >>>> > Best,> ><br>> > Brian vdB <br>> <br>> <br>> Adam,<br>> <br>> As you can see from the above, the way hotmail is formatting things <br>> makes the conversation a bit tricky :-) I'm only willing to spend so <br>> much time trying to sort through it, so I hope what follows helps.<br>> <br>> >>> data = raw_input("Feed me!")<br>> Feed me!42<br>> <br>> This calls the builtin function raw_input with a parameter setting the <br>> prompt to "Feed me!" and assigns the result to data. Since I hit 42 <br>> and then enter,<br>> <br>> >>> data<br>> '42'<br>> <br>> Notice the quotes around 42. They indicate that the value of data is a <br>> string. That's what this tells us:<br>> <br>> >>> type(data)<br>> <type 'str'><br>> <br>> The string '42' is not the same as the integer 42:<br>> <br>> >>> type(42)<br>> <type 'int'><br>> >>> '42' == 42<br>> False<br>> <br>> So, when you had an if test that was something like:<br>> <br>> if given == 1:<br>> # Do stuff here<br>> <br>> the equality comparison was never going to work---given was a string <br>> returned by raw_input and no string is ever equal to an integer.<br>> <br>> What I suggested was taking the string returned by raw_input and <br>> feeding it to int() to transform it from a string to an integer, and <br>> allow your if test to stand a chance:<br>> <br>> >>> data = raw_input("Feed me!")<br>> Feed me!42<br>> >>> if data == 42:<br>> ... print "Matches!"<br>> ...<br>> >>> data = int(raw_input("Feed me!"))<br>> Feed me!42<br>> >>> if data == 42:<br>> ... print "Matches!"<br>> ...<br>> Matches!<br>> >>><br>> <br>> There are other ways, for instance:<br>> <br>> >>> data = raw_input("Feed me!")<br>> Feed me!42<br>> >>> if data == '42':<br>> ... print "Matches!"<br>> ...<br>> Matches!<br>> >>><br>> <br>> Here, instead of transforming data to an int and then testing for <br>> equality with 42, I left data as a string and tested for equality with <br>> the string '42'.<br>> <br>> The way calling int() is a bit better, I think. If the user enters a <br>> few spaces, then 42 then a few more spaces, that way will still work:<br>> <br>> >>> data = int(raw_input("Feed me!"))<br>> Feed me! 42<br>> >>> if data == 42:<br>> ... print "Matches!"<br>> ...<br>> Matches!<br>> >>><br>> <br>> because<br>> <br>> >>> int(' 42 ')<br>> 42<br>> >>><br>> <br>> whereas<br>> <br>> >>> ' 42 ' == '42'<br>> False<br>> <br>> <br>> I hope there is some help in there somewhere :-)<br>> <br>> Brian vdB<br><br /><hr />Create the ultimate e-mail address book. Import your contacts to Windows Live Hotmail. <a href='www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=RMT_TAGLM_HMWL_reten_impcont_0507' target='_new'>Try it!</a></body>
</html>