Re: [Tutor] trouble with if
"Bryan Fodness" <[EMAIL PROTECTED]> wrote >I cannot get this to work either. Brian, when you post please tell us exactly what does not work. If there is an error message send the whole error there is a lot of useful information in them. Otherwise we have to read your code and guess what might be happening. We need both code and error text. The easier you make it for the tutors the more likely you are to get a response! > woffaxis = 7 > > if woffaxis != 0: > woaf_pos = input("What is Wedge Direction (N/A, Lateral, Towards > Heal, > Towards Toe)?") Use raw_input() and convert the result rather than input(). Especially when the input is a string anyhow. Otherwise you are leaving yourself open to scurity breaches and even accidental damage to your data due to mistyping. This is because input() tries to interpret its value as a Python expression. Thus if the user enters a string Pyhon tries to evaluate that string. Unless the usser has put quotes around their input it will probably fail. I suspect that is your problem but without an error message I can't be sure. > if woaf_pos == 'Towards Toe': > woffaxis = woffaxis > elif woaf_pos == 'Towards Heal': > woffaxis = (woffaxis * -1) > else: > woffaxis = 0 Incidentally, you could replace this set of if statements with a dictionary lookup: responses = {"Towards Toe": 1, "Towards Head": -1} woffaxis *= responses.get(woaf_pos,0) Another tip is that when comparing user input to a string its usually better to have the master strings as all uppercase or all lowercase and then convert the input string to all lower case or all upper case as appropriate, and then compare. It reduces user frustration over simple typos. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with if
I think you need to use "raw_input" instead of "input". input "eval" the input expression while "raw_input" just stores it. I find the module help very handy when I am in doubt. >>> print raw_input.__doc__ raw_input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. >>> print input.__doc__ input([prompt]) -> value Equivalent to eval(raw_input(prompt)). On 10/26/07, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > I cannot get this to work either. > > woffaxis = 7 > > if woffaxis != 0: > woaf_pos = input("What is Wedge Direction (N/A, Lateral, Towards > Heal, Towards Toe)?") > > if woaf_pos == 'Towards Toe': > woffaxis = woffaxis > elif woaf_pos == 'Towards Heal': > woffaxis = (woffaxis * -1) > else: > woffaxis = 0 > > > > > > > On 10/24/07, John Fouhy <[EMAIL PROTECTED]> wrote: > > > > On 25/10/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > > I have the following code, it keeps giving me a value of 1 for e. > > > > > > for line in file('21Ex6MV_oaf.dat'): > > > oa, openoa, w15, w30, w45, w60 = line.split() > > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and > > unblockedFS > > > > 15): > > > e = float(openoa) > > > else: > > > e = 1 > > > > > > If I comment out the else, I get the correct value > > > > > > for line in file('21Ex6MV_oaf.dat'): > > > oa, openoa, w15, w30, w45, w60 = line.split() > > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and > > unblockedFS > > > > 15): > > > e = float(openoa) > > > #else: > > > #e = 1 > > > > Maybe you need a 'break' statement after 'e = float(openoa)'? > > > > As written, e will have whatever value is appropriate for the last > > line of your input file. > > > > -- > > John. > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Aditya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with if
I cannot get this to work either. woffaxis = 7 if woffaxis != 0: woaf_pos = input("What is Wedge Direction (N/A, Lateral, Towards Heal, Towards Toe)?") if woaf_pos == 'Towards Toe': woffaxis = woffaxis elif woaf_pos == 'Towards Heal': woffaxis = (woffaxis * -1) else: woffaxis = 0 On 10/24/07, John Fouhy <[EMAIL PROTECTED]> wrote: > > On 25/10/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > > I have the following code, it keeps giving me a value of 1 for e. > > > > for line in file('21Ex6MV_oaf.dat'): > > oa, openoa, w15, w30, w45, w60 = line.split() > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > > > 15): > > e = float(openoa) > > else: > > e = 1 > > > > If I comment out the else, I get the correct value > > > > for line in file('21Ex6MV_oaf.dat'): > > oa, openoa, w15, w30, w45, w60 = line.split() > > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > > > 15): > > e = float(openoa) > > #else: > > #e = 1 > > Maybe you need a 'break' statement after 'e = float(openoa)'? > > As written, e will have whatever value is appropriate for the last > line of your input file. > > -- > John. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with if
On 25/10/2007, Bryan Fodness <[EMAIL PROTECTED]> wrote: > I have the following code, it keeps giving me a value of 1 for e. > > for line in file('21Ex6MV_oaf.dat'): > oa, openoa, w15, w30, w45, w60 = line.split() > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > 15): > e = float(openoa) > else: > e = 1 > > If I comment out the else, I get the correct value > > for line in file('21Ex6MV_oaf.dat'): > oa, openoa, w15, w30, w45, w60 = line.split() > if (float(oa) == round(offaxis)) and (eff_depth < 10 and unblockedFS > > 15): > e = float(openoa) > #else: > #e = 1 Maybe you need a 'break' statement after 'e = float(openoa)'? As written, e will have whatever value is appropriate for the last line of your input file. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
Right above the empty reply box is a "reply to all" link. Hit it, and you're good to go. On 5/30/07, Adam Urbas <[EMAIL PROTECTED]> wrote: Dang it... I am really going to have to figure out how to reply all. The cc thing only worked once and now I'm still sending to you. On 5/30/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > I started to read Alan Gauld's tutorial. The problem is, once I get > past the very basics of something, I tend to get impatient and don't > want to go back and have to redo them, but the other problem is, I may > need something that is taught in the basic sections. So ya, I'll try > to keep on a reading Alan's tutorial. > > On 5/30/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > I have already subscribed. I tried sending a message when I was not > > yet subscribed, and the Moderator or Administrator, or whoever said to > > resubscribe. Sorry about my accident programming. > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > ok well, I'm testing to see if the CC thing worked. > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > I'll try the CC thing. > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > Well, Brian, I am now very sure that we have different versions of > > > > > gmail, because on both the Quick Reply and the full reply screens, > > > > > there are no Reply buttons, or downpointing arrows. > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > What is the actual command to exit the program. I tried exit, > which > > > > > > turned purple, so I know that does something. > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > No I don't think that worked either, because now it has a > problem > > > with > > > > > > > print. > > > > > > > > > > > > > > Please help. > > > > > > > > > > > > > > Au > > > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > > I'm having trouble with the parentheses after the def thing(). > > > IDLE > > > > > > > > says that there is something wrong with it. If I type > something > > > > > > > > between them, it says that there is something wrong with the > > > > quotation > > > > > > > > marks. If I just leave it like (), then it says that > something > > is > > > > > > > > wrong with what is after the parentheses. Unless my code is > > > > supposed > > > > > > > > to go between the parentheses. I'll try that. > > > > > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > > > In the def welcome(), what do you put in the parentheses? > > > Another > > > > > > > > > question, what code do you use for ending the program. I > want > > > the > > > > > > > > > user to be able to cancel the program from the main menu, > > where > > > it > > > > > > > > > asks you to choose circle, square, etc. Or even perhaps > allow > > > the > > > > > > > > > user to go back to a previous menu, well I suppose that > would > > be > > > > the > > > > > > > > > def thing() code. But what if they were at the part where > the > > > > > program > > > > > > > > > was asking them to input the radius, how would I give them > the > > > > > option > > > > > > > > > of returning to the list of given measurements of a circle? > > > > > > > > > > > > > > > > > > On 5/29/07, Brian van den Broek <[EMAIL PROTECTED]> > wrote: > > > > > > > > > > adam urbas said unto the world upon 05/29/2007 12:39 PM: > > > > > > > > > > > The scary part is, I think I understand this. I copied > > your > > > > > last > > > > > > > > > > > example and put it in IDLE and it doesn't like you code. > > > > Never > > > > > > > > > > > mind. I figured it out. So that is so it will notify > you > > > if > > > > > your > > > > > > > > > > > choice is invalid. Nice lil tidbit of information > there. > > > > I'll > > > > > be > > > > > > > > > > > sure to use this. Oh and while your here, I'd like to > ask > > > > about > > > > > > > > > > > loops I guess they are. I want to have the program go > > back > > > to > > > > > the > > > > > > > > > > > part where it asks for the user to select an option > after > > it > > > > has > > > > > > > > > > > run one of its if statements.Like, when the user tells > it, > > > > > > > > > > > "circle," then "radius," then enters the radius: here I > > > would > > > > > like > > > > > > > > > > > the program to go back and ask the user if they want to > do > > > > > > anything > > > > > > > > > > > else, like find the area of a square, instead of the > > circle. > > > > > > Would > > > > > > > > > > > I have to tell python to print all those selections > again, > > > or > > > > > > would > > > > > > > > > > > there be a way to just return to the > beginning?Thanks,Au> > > > > Date: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Hi Adam, > > > > > > > > > > > > > > > > > > > > Again, I cut the mess, but I expect that if you use the > > gmail > > > > > > account > > > > > >
Re: [Tutor] trouble with "if"
Dang it... I am really going to have to figure out how to reply all. The cc thing only worked once and now I'm still sending to you. On 5/30/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > I started to read Alan Gauld's tutorial. The problem is, once I get > past the very basics of something, I tend to get impatient and don't > want to go back and have to redo them, but the other problem is, I may > need something that is taught in the basic sections. So ya, I'll try > to keep on a reading Alan's tutorial. > > On 5/30/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > I have already subscribed. I tried sending a message when I was not > > yet subscribed, and the Moderator or Administrator, or whoever said to > > resubscribe. Sorry about my accident programming. > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > ok well, I'm testing to see if the CC thing worked. > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > I'll try the CC thing. > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > Well, Brian, I am now very sure that we have different versions of > > > > > gmail, because on both the Quick Reply and the full reply screens, > > > > > there are no Reply buttons, or downpointing arrows. > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > What is the actual command to exit the program. I tried exit, > which > > > > > > turned purple, so I know that does something. > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > No I don't think that worked either, because now it has a > problem > > > with > > > > > > > print. > > > > > > > > > > > > > > Please help. > > > > > > > > > > > > > > Au > > > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > > I'm having trouble with the parentheses after the def thing(). > > > IDLE > > > > > > > > says that there is something wrong with it. If I type > something > > > > > > > > between them, it says that there is something wrong with the > > > > quotation > > > > > > > > marks. If I just leave it like (), then it says that > something > > is > > > > > > > > wrong with what is after the parentheses. Unless my code is > > > > supposed > > > > > > > > to go between the parentheses. I'll try that. > > > > > > > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > > > > In the def welcome(), what do you put in the parentheses? > > > Another > > > > > > > > > question, what code do you use for ending the program. I > want > > > the > > > > > > > > > user to be able to cancel the program from the main menu, > > where > > > it > > > > > > > > > asks you to choose circle, square, etc. Or even perhaps > allow > > > the > > > > > > > > > user to go back to a previous menu, well I suppose that > would > > be > > > > the > > > > > > > > > def thing() code. But what if they were at the part where > the > > > > > program > > > > > > > > > was asking them to input the radius, how would I give them > the > > > > > option > > > > > > > > > of returning to the list of given measurements of a circle? > > > > > > > > > > > > > > > > > > On 5/29/07, Brian van den Broek <[EMAIL PROTECTED]> > wrote: > > > > > > > > > > adam urbas said unto the world upon 05/29/2007 12:39 PM: > > > > > > > > > > > The scary part is, I think I understand this. I copied > > your > > > > > last > > > > > > > > > > > example and put it in IDLE and it doesn't like you code. > > > > Never > > > > > > > > > > > mind. I figured it out. So that is so it will notify > you > > > if > > > > > your > > > > > > > > > > > choice is invalid. Nice lil tidbit of information > there. > > > > I'll > > > > > be > > > > > > > > > > > sure to use this. Oh and while your here, I'd like to > ask > > > > about > > > > > > > > > > > loops I guess they are. I want to have the program go > > back > > > to > > > > > the > > > > > > > > > > > part where it asks for the user to select an option > after > > it > > > > has > > > > > > > > > > > run one of its if statements.Like, when the user tells > it, > > > > > > > > > > > "circle," then "radius," then enters the radius: here I > > > would > > > > > like > > > > > > > > > > > the program to go back and ask the user if they want to > do > > > > > > anything > > > > > > > > > > > else, like find the area of a square, instead of the > > circle. > > > > > > Would > > > > > > > > > > > I have to tell python to print all those selections > again, > > > or > > > > > > would > > > > > > > > > > > there be a way to just return to the > beginning?Thanks,Au> > > > > Date: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Hi Adam, > > > > > > > > > > > > > > > > > > > > Again, I cut the mess, but I expect that if you use the > > gmail > > > > > > account > > > > > > > > > > you just posted about here on in, that will be the end of > > it. > > > > > > > > > > > > > > > > > > > > I'm glad that you are startin
Re: [Tutor] trouble with "if"
ok well, I'm testing to see if the CC thing worked. On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > I'll try the CC thing. > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > Well, Brian, I am now very sure that we have different versions of > > gmail, because on both the Quick Reply and the full reply screens, > > there are no Reply buttons, or downpointing arrows. > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > What is the actual command to exit the program. I tried exit, which > > > turned purple, so I know that does something. > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > No I don't think that worked either, because now it has a problem with > > > > print. > > > > > > > > Please help. > > > > > > > > Au > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > I'm having trouble with the parentheses after the def thing(). IDLE > > > > > says that there is something wrong with it. If I type something > > > > > between them, it says that there is something wrong with the > quotation > > > > > marks. If I just leave it like (), then it says that something is > > > > > wrong with what is after the parentheses. Unless my code is > supposed > > > > > to go between the parentheses. I'll try that. > > > > > > > > > > On 5/29/07, Adam Urbas <[EMAIL PROTECTED]> wrote: > > > > > > In the def welcome(), what do you put in the parentheses? Another > > > > > > question, what code do you use for ending the program. I want the > > > > > > user to be able to cancel the program from the main menu, where it > > > > > > asks you to choose circle, square, etc. Or even perhaps allow the > > > > > > user to go back to a previous menu, well I suppose that would be > the > > > > > > def thing() code. But what if they were at the part where the > > program > > > > > > was asking them to input the radius, how would I give them the > > option > > > > > > of returning to the list of given measurements of a circle? > > > > > > > > > > > > On 5/29/07, Brian van den Broek <[EMAIL PROTECTED]> wrote: > > > > > > > adam urbas said unto the world upon 05/29/2007 12:39 PM: > > > > > > > > The scary part is, I think I understand this. I copied your > > last > > > > > > > > example and put it in IDLE and it doesn't like you code. > Never > > > > > > > > mind. I figured it out. So that is so it will notify you if > > your > > > > > > > > choice is invalid. Nice lil tidbit of information there. > I'll > > be > > > > > > > > sure to use this. Oh and while your here, I'd like to ask > about > > > > > > > > loops I guess they are. I want to have the program go back to > > the > > > > > > > > part where it asks for the user to select an option after it > has > > > > > > > > run one of its if statements.Like, when the user tells it, > > > > > > > > "circle," then "radius," then enters the radius: here I would > > like > > > > > > > > the program to go back and ask the user if they want to do > > > anything > > > > > > > > else, like find the area of a square, instead of the circle. > > > Would > > > > > > > > I have to tell python to print all those selections again, or > > > would > > > > > > > > there be a way to just return to the beginning?Thanks,Au> > Date: > > > > > > > > > > > > > > > > > > > > > Hi Adam, > > > > > > > > > > > > > > Again, I cut the mess, but I expect that if you use the gmail > > > account > > > > > > > you just posted about here on in, that will be the end of it. > > > > > > > > > > > > > > I'm glad that you are starting to have the warm glow of > > > understanding > > > > > :-) > > > > > > > > > > > > > > What you are asking about here is one reason why functions are > so > > > > > > > useful. They allow you (more or less) to give a name to a chunk > of > > > > > > > code, and then you can rerun that chunk at will by invoking the > > > name. > > > > > > > > > > > > > > Given the problem you want to solve, I'd structure my code > > something > > > > > > > like the following. Most of the details need to be filled in, > but > > > this > > > > > > > is the skeletal structure. > > > > > > > > > > > > > > > > > > > > > def welcome_message(): > > > > > > > # Some actions to invoke when the user starts the program > > > > > > > print "Welcome to this program." > > > > > > > > > > > > > > def display_menu(): > > > > > > > # Insert code for showing the user the menu of options > > > > > > > pass > > > > > > > > > > > > > > def circle_area(): > > > > > > > # insert code here to ask user for the radius, compute the > > > area, > > > > > > > # and display the result. You might well want to divide > that > > up > > > > > > > # into other functions that this one calls. > > > > > > > pass > > > > > > > > > > > > > > def square_area(): > > > > > > > # Likewise > > > > > > > pass > > > > > > > > > > > > > > # And so on, for each shape that you wish to handle > > > > > > > > > > > > > > def exit_message():
Re: [Tutor] trouble with "if"
adam urbas said unto the world upon 05/29/2007 12:39 PM: > The scary part is, I think I understand this. I copied your last > example and put it in IDLE and it doesn't like you code. Never > mind. I figured it out. So that is so it will notify you if your > choice is invalid. Nice lil tidbit of information there. I'll be > sure to use this. Oh and while your here, I'd like to ask about > loops I guess they are. I want to have the program go back to the > part where it asks for the user to select an option after it has > run one of its if statements.Like, when the user tells it, > "circle," then "radius," then enters the radius: here I would like > the program to go back and ask the user if they want to do anything > else, like find the area of a square, instead of the circle. Would > I have to tell python to print all those selections again, or would > there be a way to just return to the beginning?Thanks,Au> Date: Hi Adam, Again, I cut the mess, but I expect that if you use the gmail account you just posted about here on in, that will be the end of it. I'm glad that you are starting to have the warm glow of understanding :-) What you are asking about here is one reason why functions are so useful. They allow you (more or less) to give a name to a chunk of code, and then you can rerun that chunk at will by invoking the name. Given the problem you want to solve, I'd structure my code something like the following. Most of the details need to be filled in, but this is the skeletal structure. def welcome_message(): # Some actions to invoke when the user starts the program print "Welcome to this program." def display_menu(): # Insert code for showing the user the menu of options pass def circle_area(): # insert code here to ask user for the radius, compute the area, # and display the result. You might well want to divide that up # into other functions that this one calls. pass def square_area(): # Likewise pass # And so on, for each shape that you wish to handle def exit_message(): # Some actions to invoke when the user chooses to terminate # the program. print "Thank you for using this program. Goodbye." def prompt_user(): # Here is where the sort of code I showed you before would go. # I'd include an option, say 0, for exiting, which, when the # user picks it, you call exit_message() while True: try: choice = int(raw_input("Please make your choice ")) if choice < 0 or choice > 2: # Adjust to suit options raise ValueError break except ValueError: print "Please make a choice from the options offered." # sends the choice back to the code that called prompt_user # We won't get here until a good choice has been made return choice def main(): # The main function driving your program. It might look # something like this: welcome_message() while True: # This will loop forever until you break out display_menu() choice = prompt_user() if choice == 0: exit_message() break # Terminate the while loop elif choice == 1: # Assuming 1 was the option for circle circle_area() elif choice == 2: square_area() # And so on print "Please make another choice:" # Go back to top of loop if __name__ == '__main__': # This will run if you run the script, but not if you import it. main() This has not been tested (it is only an outline) but it does pass the only so reliable eyeball check :-) I'd suggest you try filling this sketch out to be useful, and post if you run into troubles. Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
The scary part is, I think I understand this. I copied your last example and put it in IDLE and it doesn't like you code. Never mind. I figured it out. So that is so it will notify you if your choice is invalid. Nice lil tidbit of information there. I'll be sure to use this. Oh and while your here, I'd like to ask about loops I guess they are. I want to have the program go back to the part where it asks for the user to select an option after it has run one of its if statements.Like, when the user tells it, "circle," then "radius," then enters the radius: here I would like the program to go back and ask the user if they want to do anything else, like find the area of a square, instead of the circle. Would I have to tell python to print all those selections again, or would there be a way to just return to the beginning?Thanks,Au> Date: Sun, 27 May 2007 15:10:08 -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC: tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam urbas said unto the world upon 05/27/2007 01:49 PM:> > 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.> > readable>> > > Hi Adam and all,> > Adam was asking about how to use raw_input to drive a basic command > prompt menu system. I'd tried to explain that raw_input returns > strings, so his if tests which were something like:> > choice = raw_input("Enter an option)> if choice == 1:> do_option_1_stuff()> elif choice == 2:> do_option_2_stuff()> > were not going to work, as choice will never be equal to an int.> > I'd sketched a few ways to deal with this, chiefly applying int() to > choice or comparing choice to '1', etc.> > That's more of less the gist of the above snippage and takes us more > or less up to the point where Adam asked his question above.> > I'm going to show you a few things that might be new to you, Adam. > Let's build up in steps.> > As a first pass, I would do the following:> > choice = int(raw_input("Please make your choice "))> > if choice == 1:> # Option 1 code here> print "In option 1"> > elif choice == 2:> # Option 2 code here> print "In option 2"> > # Carry on if-test as needed (or until you get to the point> # of learning about dictionary dispatch :-)> > That will be fine, until your user enters something silly:> > >>>> Please make your choice I like bikes!> Traceback (most recent call last):>File "/home/brian/docs/jotter/python_scraps/adamcode.py", line 1, > in > choice = int(raw_input("Please make your choice "))> ValueError: invalid literal for int() with base 10: 'I like bikes!'> >>>> > That's no good!> > So, we can use Python's exception handling tools to make this a bit > better.> > > try:> choice = int(raw_input("Please make your choice "))> except ValueError:> print "Please make a choice from the options offered."> > > if choice == 1:> print "In option 1"> > elif choice == 2:> print "In option 2"> > > There is still a problem, though:> > >>> # Make sure the previous value assigned to choice is gone.> >>> del(choice)> >>>> Please make your choice I like Bikes> Please make a choice from the options offered.> Traceback (most recent call last):>File "/home/brian/docs/jotter/python_scraps/adamcode.py", line 7, > in > if choice == 1:> NameError: name 'choice' is not defined> >>>> > We've printed the reminder to the user, but then have gone on to > compare the non-existent choice value to 1, and that doesn't work so > well. It isn't enough to make sure that choice isn't insane---we need > to make sure that there is a choice value at all.> > So, better still:> > > while True:> try:> choice = int(raw_input("Please make your choice "))> # If the previous line worked, end the while loop. If it did> # not work, we won't get here, so the loop will keep looping.> break> except ValueError:> print "Please make a choice from the options offered."> > if choice == 1:> print "In option 1"> > elif choice == 2:> print "In option 2"> > > Now we get the following:> > Please make your choice I like bikes!> Please make a
Re: [Tutor] trouble with "if"
* Rikard Bosnjakovic (Mon, 28 May 2007 17:55:42 +0200) > On 5/28/07, Thorsten Kampe <[EMAIL PROTECTED]> wrote: > > Do you really think someone can or will read what you wrote? I've > > never seen something so horribly formatted like you emails - and I've > > seen lots of awful formatted emails... > > Looks fine at my end. As Brian van den Broek said[1] to the "Hotmail" guy: "". Maybe because he's posting HTML and the text part is complete crap. *Hotmail* *gnarrf*. Thorsten [1] http://permalink.gmane.org/gmane.comp.python.tutor/40742 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
On 5/28/07, Thorsten Kampe <[EMAIL PROTECTED]> wrote: > Do you really think someone can or will read what you wrote? I've > never seen something so horribly formatted like you emails - and I've > seen lots of awful formatted emails... Looks fine at my end. -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
adam urbas wrote: > Very frustrating. What is a non-int and what is 'str'? Why can't it > multiply the sequence? I guess I should include the program I'm using > for these things. These are more examples of the same kinds of errors you have been having. Values in Python have a type. Some examples of types are int (integer), float (floating point number) and str (string). Each type supports different operations, for example you can't add 5 to 'this is a string' or multiply 'a' * 'b' or even '5' * '6', both are strings. > I'm having this problem with both of these attached. The messages above > are from area.py. area.py is sort of a prototype of radiacir.py, a test > version. You know, I should probably try that int trick, which I seem > to have forgotten. And guess what that did it. It's amazing when you > apply the things that you learn. Apparently I am quite absent minded. > Well It seems I don't need any of this help anymore. Oh well. Thanks > anyway. You really should take the time to understand what is going on here. int() is not a 'trick'. If you approach programming as trying a bunch of tricks until you get something that seems to work, your programs will be build on sand. If you take the time to understand and work with the model that the programming language presents, you will have a much easier time of it. There are many good books and tutorials available. I recommend the book "Python Programming for the absolute beginner" for someone with no previous programming experience: http://premierpressbooks.com/ptr_detail.cfm?group=Programming&subcat=Other%20Programming&isbn=1%2D59863%2D112%2D8 Quite a few beginners' tutorials are listed here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Please, pick one of these resources, read it, write small programs that use what you learn, come back here to ask questions when you get stuck. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
* adam urbas (Sun, 27 May 2007 23:42:01 -0500) > You don't know what a Ti 83 is. Calculator. The most basic programming > available. It already has so many functions built into it that it is much > easier to tell it to do things. You don't have to do all this integer > conversion and such whatnot. Wow... I'm really unsure of how this thing is > supposed to work. It seems the more I learn about Python, the more confused I become. It's enough to bring tears to your eyes. Not really but ya.Someone else helped me with the problem of accepting numbers and words. I used:if shape in["1","circle"]:something like that. It works wonderfully. I'm not sure why, but I know that it does and that is enough. Someone else also said that I had to convert to int, and I did. That was another problem, which is now fixed.But, as usual, it is just one problem after another. Now I have run into this error message: Traceback (most recent call last): File "C:\Documents and Settings\HP_Owner\Python0\area.py", line 23, in area = 3.14*(radius**2)TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'>>> and others like this:Traceback (most recent call last): File "C:\Documents and Settings\HP_Owner\Python0\area.py", line 19, in area = height*widthTypeError: can't multiply sequence by non-int of type 'str'>>> Very frustrating. What is a non-int and what is 'str'? Why can't it multiply the sequence? I guess I should include the program I'm using for these things.I'm having this problem with both of these attached. The messages above are from area.py. area.py is sort of a prototype of radiacir.py, a test version. You know, I should probably try that int trick, which I seem to have forgotten. And guess what that did it. It's amazing when you apply the things that you learn. Apparently I am quite absent minded. Well It seems I don't need any of this help anymore. Oh well. Thanks anyway.Au > To: tutor@python.org> From: [EMAIL PROTECTED]> Date: Thu, 24 May 2007 23:34:05 +0100> Subject: Re: [Tutor] trouble with "if"> > "adam urbas" <[EMAIL PROTECTED]> wrote > > > It won't even accept words. > > I can only get it to accept numbers. > > try this(untested code!):> > number = None> data = raw_input('Type something: ')> try: number = int(data)> except: data = data.split()# assume a string> > if number:# user entered a number> if number == 1: print 'circle'> elif number == 2: print 'another'> else: # user entered words> if data[0].lower() == 'circle': print 'circle'> else: print 'user entered ', data[0]> > Notice that to use ithe input as a number you have to > convert the raw input characters to a number (using int)> To get the individual words we can use split() which by > default splits a string into the individual words.> > Is that the kind of thing you mean?> > I've no idea what a Ti83 is BTW. :-)> > Alan G.> > ___> Tutor maillist - Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor Do you really think someone can or will read what you wrote? I've never seen something so horribly formatted like you emails - and I've seen lots of awful formatted emails... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
I thank you much Alan. This has been very helpful already and I'm only on page 2. The world needs more newb-friendly people like you.> To: tutor@python.org> From: [EMAIL PROTECTED]> Date: Thu, 24 May 2007 23:39:41 +0100> Subject: Re: [Tutor] trouble with "if"> > Hi adam. > > With the aid of Google it seems a Ti83 is a programmable calculator.> > I'm not sure what python tutor you are using but it looks like > you need to cover some very basic stuff around data types.> > You may find the Raw Materials topic in my tutor useful to give > you a feel for the different types of data in Python.> > The Talking to the User topic will cover the use of raw_input.> > And the Branching topic has an examplre very similar to what > you are trying to do.> > HTH,> > -- > Alan Gauld> Author of the Learn to Program web site> http://www.freenetpages.co.uk/hp/alan.gauld> > ___> Tutor maillist - Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor _ Download Messenger. Start an i’m conversation. Support a cause. Join now. http://im.live.com/messenger/im/home/?source=TAGWL_MAY07___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
You don't know what a Ti 83 is. Calculator. The most basic programming available. It already has so many functions built into it that it is much easier to tell it to do things. You don't have to do all this integer conversion and such whatnot. Wow... I'm really unsure of how this thing is supposed to work. It seems the more I learn about Python, the more confused I become. It's enough to bring tears to your eyes. Not really but ya.Someone else helped me with the problem of accepting numbers and words. I used:if shape in["1","circle"]:something like that. It works wonderfully. I'm not sure why, but I know that it does and that is enough. Someone else also said that I had to convert to int, and I did. That was another problem, which is now fixed.But, as usual, it is just one problem after another. Now I have run into this error message: Traceback (most recent call last): File "C:\Documents and Settings\HP_Owner\Python0\area.py", line 23, in area = 3.14*(radius**2)TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'>>> and others like this:Traceback (most recent call last): File "C:\Documents and Settings\HP_Owner\Python0\area.py", line 19, in area = height*widthTypeError: can't multiply sequence by non-int of type 'str'>>> Very frustrating. What is a non-int and what is 'str'? Why can't it multiply the sequence? I guess I should include the program I'm using for these things.I'm having this problem with both of these attached. The messages above are from area.py. area.py is sort of a prototype of radiacir.py, a test version. You know, I should probably try that int trick, which I seem to have forgotten. And guess what that did it. It's amazing when you apply the things that you learn. Apparently I am quite absent minded. Well It seems I don't need any of this help anymore. Oh well. Thanks anyway.Au > To: tutor@python.org> From: [EMAIL PROTECTED]> Date: Thu, 24 May 2007 23:34:05 +0100> Subject: Re: [Tutor] trouble with "if"> > "adam urbas" <[EMAIL PROTECTED]> wrote > > > It won't even accept words. > > I can only get it to accept numbers. > > try this(untested code!):> > number = None> data = raw_input('Type something: ')> try: number = int(data)> except: data = data.split()# assume a string> > if number:# user entered a number> if number == 1: print 'circle'> elif number == 2: print 'another'> else: # user entered words> if data[0].lower() == 'circle': print 'circle'> else: print 'user entered ', data[0]> > Notice that to use ithe input as a number you have to > convert the raw input characters to a number (using int)> To get the individual words we can use split() which by > default splits a string into the individual words.> > Is that the kind of thing you mean?> > I've no idea what a Ti83 is BTW. :-)> > Alan G.> > ___> Tutor maillist - Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor _ Create the ultimate e-mail address book. Import your contacts to Windows Live Hotmail. www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507#"Area calculation program" print "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 users choice:" shape = raw_input("> ") #"Calculate the area:" if shape in["1","rectangle"]: height = raw_input("Please enter the height: ") width = raw_input("Please enter the width: ") area = height*width print "The area is", area if shape in["2","circle"]: radius = raw_input("Please enter the radius: ") area = 3.14*(radius**2) print "The area is", area #"Circle Data Calculation Program:" print "Welcome to the Circle Data Calcuation Program." print #"Menu 1:" print "Pick a shape:" print "(NOTE: You must select the number of the shape and not the shape itself)" print "1 Circle" print "2 Square" print "3 Triangle" #"User's Choice:" shape=raw_input("> ") #"Select Given:" if shape == "1" or shape == "circle": print "Choose the
Re: [Tutor] trouble with "if"
adam urbas said unto the world upon 05/27/2007 01:49 PM: > 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. Hi Adam and all, Adam was asking about how to use raw_input to drive a basic command prompt menu system. I'd tried to explain that raw_input returns strings, so his if tests which were something like: choice = raw_input("Enter an option) if choice == 1: do_option_1_stuff() elif choice == 2: do_option_2_stuff() were not going to work, as choice will never be equal to an int. I'd sketched a few ways to deal with this, chiefly applying int() to choice or comparing choice to '1', etc. That's more of less the gist of the above snippage and takes us more or less up to the point where Adam asked his question above. I'm going to show you a few things that might be new to you, Adam. Let's build up in steps. As a first pass, I would do the following: choice = int(raw_input("Please make your choice ")) if choice == 1: # Option 1 code here print "In option 1" elif choice == 2: # Option 2 code here print "In option 2" # Carry on if-test as needed (or until you get to the point # of learning about dictionary dispatch :-) That will be fine, until your user enters something silly: >>> Please make your choice I like bikes! Traceback (most recent call last): File "/home/brian/docs/jotter/python_scraps/adamcode.py", line 1, in choice = int(raw_input("Please make your choice ")) ValueError: invalid literal for int() with base 10: 'I like bikes!' >>> That's no good! So, we can use Python's exception handling tools to make this a bit better. try: choice = int(raw_input("Please make your choice ")) except ValueError: print "Please make a choice from the options offered." if choice == 1: print "In option 1" elif choice == 2: print "In option 2" There is still a problem, though: >>> # Make sure the previous value assigned to choice is gone. >>> del(choice) >>> Please make your choice I like Bikes Please make a choice from the options offered. Traceback (most recent call last): File "/home/brian/docs/jotter/python_scraps/adamcode.py", line 7, in if choice == 1: NameError: name 'choice' is not defined >>> We've printed the reminder to the user, but then have gone on to compare the non-existent choice value to 1, and that doesn't work so well. It isn't enough to make sure that choice isn't insane---we need to make sure that there is a choice value at all. So, better still: while True: try: choice = int(raw_input("Please make your choice ")) # If the previous line worked, end the while loop. If it did # not work, we won't get here, so the loop will keep looping. break except ValueError: print "Please make a choice from the options offered." if choice == 1: print "In option 1" elif choice == 2: print "In option 2" Now we get the following: Please make your choice I like bikes! Please make a choice from the options offered. Please make your choice Please take this Please make a choice from the options offered. Please make your choice1 In option 1 >>> There is still a problem, though: Please make your choice 42 >>> Our sanity check has only insisted that the user enter a value that can be turned into an int; nothing as yet makes it be one of the ints we are expecting. So, try this: while True: try: choice = int(raw_input("Please make your choice ")) if choice < 1 or choice > 2: # Adjust to suit options raise ValueError break except ValueError: print "Please make a choice from the options offered." if choice == 1: print "In option 1" elif choice == 2: print "In option 2" Please make your choice I like bikes! Please make a choice from the options offered. Please make your choice 42 Please make a choice from the options offered. Please make your choice 2 In option 2 >>> Now, all of this should be formatted to be a bit prettier---and displaying the allowable options up front is a good idea, too---but the essential ideas are there. There might be some parts of this that are new to you, so ask away if you've gotten a bit lost. And, I'm no expert, so if someone else comes along and says `No, don't do it like that', odds are they might be right. (Especially if their name is Alan, Danny, or Kent ;-) Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
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.> Date: Wed, 23 May 2007 13:40:09 -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC: tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam urbas said unto the world upon 05/23/2007 01:04 PM:> > Sorry, I don't think Hotmail has turn off HTML. If it does I> > havn't been able to find it. I think you're going to have to> > explain your little bit of text stuff down there at the bottom. I> > have no idea what most of that means. All my choice things are> > working now though. I think that is what you were trying to help> > me with. What I used wasif shape in["1","circle"]:and if shape ==> > "1" or shape =="circle":It works perfectly fine now.Ya that little> > bit o' code is really puzzling. I wish I knew more about this> > python deal. I understand the concept, but not the rules or the> > techniques and things of that sort. OK... I've got it... the> > data=raw_input('Feed Me!'). Ok I now understand that bit. Then it> > says Feed Me! and you put 42 (the ultimate answer to life the> > universe, everything). OK, it won't accept the bit.> > it doesn't like the "<". Well, I just removed that bit and it> > said:Feed Me! and I put 42, and it said >>> (I guess it's> > satisfied now, with the whole feeding). Well if I understood what> > 'str' meant, then I could probably figure the rest out. Well I> > have to go do other things so I'll save the rest of this figuring> > out till later.I shall return,Adam> Date: Wed, 23 May 2007 12:12:16> > -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC:> > tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam> > urbas said unto the world upon 05/23/2007 11:57 AM:> > > > Hi all,>> > > > > I've been working with this new program that I wrote. I> > started out > > with it on a Ti-83, which is much easier to program> > than python. Now > > I'm trying to transfer the program to python> > but its proving to be quite > > difficult. I'm not sure what the> > whole indentation thing is for. And > > now I'm having trouble> > with the if statement things. > > > > #"Circle Data Calculation> > Program:"> > print "Welcome to the Circle Data Calcuation> > Program."> > print> > > > #"Menu 1:"> > print "Pick a shape:">> > > print "(NOTE: You must select the number of the shape and not the> > shape > > itself)"> > print "1 Circle"> > print "2 Square"> > print> > "3 Triangle"> > > > #"User's Choice:"> > shape=raw_input("> ")>> > > > > #"Select Given:"> > if shape == 1:> > print> > "Choose the given value:"> > print "1 radius"> >> > print "2 diameter"> > print "3 circumference"> >> > print "4 area"> > > > #"User's Choice:"> > given=raw_input("> ")> >> > > > if given == 1:> > radius=raw_input("Enter Radius:")> >> > diameter=(radius*2)> > circumference=(diameter*3.14)> >> > area=(radius**2*3.14)> > print "Diameter:", diameter> >> > print "Circumference:", circumference> > print "Area:",> > area> > > > if given == 2:> > diameter=raw_input("Enter> > Diameter:")> > radius=(diameter/2)> >> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> >> > print "Radius:", radius> > print "Circumference:",> > circumference> > print "Area:", area> > > > if given == 3:>> > > circumference=raw_input("Enter Circumference:")> >> > radius=(circumference/3.14/2)> > diameter=(radius*2)> >> > area=(radius**2*3.14)> > print "Radius:", radius> >> > print "Diameter:", diameter> > print "Area:", area> > > >> > if given == 4:> > area=raw_input(&
Re: [Tutor] trouble with "if"
Hi adam. With the aid of Google it seems a Ti83 is a programmable calculator. I'm not sure what python tutor you are using but it looks like you need to cover some very basic stuff around data types. You may find the Raw Materials topic in my tutor useful to give you a feel for the different types of data in Python. The Talking to the User topic will cover the use of raw_input. And the Branching topic has an examplre very similar to what you are trying to do. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
"adam urbas" <[EMAIL PROTECTED]> wrote > It won't even accept words. > I can only get it to accept numbers. try this(untested code!): number = None data = raw_input('Type something: ') try: number = int(data) except: data = data.split()# assume a string if number:# user entered a number if number == 1: print 'circle' elif number == 2: print 'another' else: # user entered words if data[0].lower() == 'circle': print 'circle' else: print 'user entered ', data[0] Notice that to use ithe input as a number you have to convert the raw input characters to a number (using int) To get the individual words we can use split() which by default splits a string into the individual words. Is that the kind of thing you mean? I've no idea what a Ti83 is BTW. :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with if
I'm not sure what the whole indentation thing is for. And now I'm having trouble with the if statement things. Maybe your if statement troubles have been solved by others by now, but I'll just add that "the indentation thing" is a vital feature of Python, it is the way to separate code blocks. Other languages uses other means, like curly braces, etc. I get the sense those who like Python enjoy indentation because it forces the code to be quite readable, and I agree. See this: http://www.diveintopython.org/getting_to_know_python/indenting_code.html Also, as mentioned previously, keep in mind that 2 is not the same as "2" and "=" is not the same as "==". The single "=" is used to assign names to objects, whereas the == is for evaluating something, so for if statements use == and not =. Also note you can put "and" along with if, so you can say if x == "mom" and y == "dad": print "my parents" and lots of other stuff. -Che _ PC Magazines 2007 editors choice for best Web mailaward-winning Windows Live Hotmail. http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_pcmag_0507 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
Hi Adam, adam urbas wrote: > when I input a radius, it says: > > can't multiply sequence by non-int of type 'float' ... > > radius=raw_input("Enter Radius:") > > diameter=(radius*2) After you collect the raw_input for the radius, the radius variable contains a string, not a number (that's what '' means). Python is calling the string a sequence in your error message. Try converting your radius to a float type first: radius=float(raw_input("Enter Radius:")) As side notes: those '>>>' characters in previous responses are what the python interactive terminal displays as its command prompt. The 'type()' function tells you the data type of a variable. Here's an example of using the Python interactive terminal to debug your issue (give it a try yourself, but don't enter the '>>>' in the terminal): [EMAIL PROTECTED]:~$ python Python 2.5.1 (r251:54863, May 3 2007, 12:27:48) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> radius=raw_input("Enter Radius:") Enter Radius:5 >>> radius '5' >>> type(radius) >>> diameter=(radius*2) >>> diameter '55' # That is, it is giving you the string '5', two times >>> type(diameter) >>> >>> >>> radius=float(raw_input("Enter Radius:")) Enter Radius:5 >>> radius # radius now contains a float type 5.0 >>> type(radius) >>> diameter=(radius*2) >>> diameter 10.0 >>> type(diameter) >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
adam urbas said unto the world upon 05/23/2007 01:04 PM: > Sorry, I don't think Hotmail has turn off HTML. If it does I > havn't been able to find it. I think you're going to have to > explain your little bit of text stuff down there at the bottom. I > have no idea what most of that means. All my choice things are > working now though. I think that is what you were trying to help > me with. What I used wasif shape in["1","circle"]:and if shape == > "1" or shape =="circle":It works perfectly fine now.Ya that little > bit o' code is really puzzling. I wish I knew more about this > python deal. I understand the concept, but not the rules or the > techniques and things of that sort. OK... I've got it... the > data=raw_input('Feed Me!'). Ok I now understand that bit. Then it > says Feed Me! and you put 42 (the ultimate answer to life the > universe, everything). OK, it won't accept the bit. > it doesn't like the "<". Well, I just removed that bit and it > said:Feed Me! and I put 42, and it said >>> (I guess it's > satisfied now, with the whole feeding). Well if I understood what > 'str' meant, then I could probably figure the rest out. Well I > have to go do other things so I'll save the rest of this figuring > out till later.I shall return,Adam> Date: Wed, 23 May 2007 12:12:16 > -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC: > tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam > urbas said unto the world upon 05/23/2007 11:57 AM:> > > > Hi all,> > > > > I've been working with this new program that I wrote. I > started out > > with it on a Ti-83, which is much easier to program > than python. Now > > I'm trying to transfer the program to python > but its proving to be quite > > difficult. I'm not sure what the > whole indentation thing is for. And > > now I'm having trouble > with the if statement things. > > > > #"Circle Data Calculation > Program:"> > print "Welcome to the Circle Data Calcuation > Program."> > print> > > > #"Menu 1:"> > print "Pick a shape:"> > > print "(NOTE: You must select the number of the shape and not the > shape > > itself)"> > print "1 Circle"> > print "2 Square"> > print > "3 Triangle"> > > > #"User's Choice:"> > shape=raw_input("> ")> > > > > #"Select Given:"> > if shape == 1:> > print > "Choose the given value:"> > print "1 radius"> > > print "2 diameter"> > print "3 circumference"> > > print "4 area"> > > > #"User's Choice:"> > given=raw_input("> ")> > > > > if given == 1:> > radius=raw_input("Enter Radius:")> > > diameter=(radius*2)> > circumference=(diameter*3.14)> > > area=(radius**2*3.14)> > print "Diameter:", diameter> > > print "Circumference:", circumference> > print "Area:", > area> > > > if given == 2:> > diameter=raw_input("Enter > Diameter:")> > radius=(diameter/2)> > > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > > print "Radius:", radius> > print "Circumference:", > circumference> > print "Area:", area> > > > if given == 3:> > > circumference=raw_input("Enter Circumference:")> > > radius=(circumference/3.14/2)> > diameter=(radius*2)> > > area=(radius**2*3.14)> > print "Radius:", radius> > > print "Diameter:", diameter> > print "Area:", area> > > > > if given == 4:> > area=raw_input("Enter Area:")> > > radius=(area/3.14)> > > > This is the whole program so > far, because I haven't quite finished it > > yet. But I tried to > get it to display another list of options after you > > select a > shape but it just does this.> > > > Pick a shape:> > 1 Circle> > 2 > Square> > 3 Triangle> > >1> > >1> > >>>> > > > I'm not sure why > it does that but I do know that it is skipping the > > second li
Re: [Tutor] trouble with "if"
Sorry, I don't think Hotmail has turn off HTML. If it does I havn't been able to find it. I think you're going to have to explain your little bit of text stuff down there at the bottom. I have no idea what most of that means. All my choice things are working now though. I think that is what you were trying to help me with. What I used wasif shape in["1","circle"]:and if shape == "1" or shape =="circle":It works perfectly fine now.Ya that little bit o' code is really puzzling. I wish I knew more about this python deal. I understand the concept, but not the rules or the techniques and things of that sort. OK... I've got it... the data=raw_input('Feed Me!'). Ok I now understand that bit. Then it says Feed Me! and you put 42 (the ultimate answer to life the universe, everything). OK, it won't accept the bit. it doesn't like the "<". Well, I just removed that bit and it said:Feed Me! and I put 42, and it said >>> (I guess it's satisfied now, with the whole feeding). Well if I understood what 'str' meant, then I could probably figure the rest out. Well I have to go do other things so I'll save the rest of this figuring out till later.I shall return,Adam> Date: Wed, 23 May 2007 12:12:16 -0400> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> CC: tutor@python.org> Subject: Re: [Tutor] trouble with "if"> > adam urbas said unto the world upon 05/23/2007 11:57 AM:> > > > Hi all,> > > > I've been working with this new program that I wrote. I started out > > with it on a Ti-83, which is much easier to program than python. Now > > I'm trying to transfer the program to python but its proving to be quite > > difficult. I'm not sure what the whole indentation thing is for. And > > now I'm having trouble with the if statement things. > > > > #"Circle Data Calculation Program:"> > print "Welcome to the Circle Data Calcuation Program."> > print> > > > #"Menu 1:"> > print "Pick a shape:"> > print "(NOTE: You must select the number of the shape and not the shape > > itself)"> > print "1 Circle"> > print "2 Square"> > print "3 Triangle"> > > > #"User's Choice:"> > shape=raw_input("> ")> > > > #"Select Given:"> > if shape == 1:> > print "Choose the given value:"> > print "1 radius"> > print "2 diameter"> > print "3 circumference"> > print "4 area"> > > > #"User's Choice:"> > given=raw_input("> ")> > > > if given == 1:> > radius=raw_input("Enter Radius:")> > diameter=(radius*2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Diameter:", diameter> > print "Circumference:", circumference> > print "Area:", area> > > > if given == 2:> > diameter=raw_input("Enter Diameter:")> > radius=(diameter/2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Circumference:", circumference> > print "Area:", area> > > > if given == 3:> > circumference=raw_input("Enter Circumference:")> > radius=(circumference/3.14/2)> > diameter=(radius*2)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Diameter:", diameter> > print "Area:", area> > > > if given == 4:> > area=raw_input("Enter Area:")> > radius=(area/3.14)> > > > This is the whole program so far, because I haven't quite finished it > > yet. But I tried to get it to display another list of options after you > > select a shape but it just does this.> > > > Pick a shape:> > 1 Circle> > 2 Square> > 3 Triangle> > >1> > >1> > >>>> > > > I'm not sure why it does that but I do know that it is skipping the > > second list of options.> > > > Another of my problems is that I can't figure out how to get it to > > accept two different inputs for a selection. Like I want it to accept > > both t
Re: [Tutor] trouble with "if"
Thanks Andre. That solved most of the problems. Now all the lists will run correctly, but when I input a radius, it says:can't multiply sequence by non-int of type 'float'When it displays that, it is talking about circumference=(radius*2*3.14). I'm guessing it doesn't want me to multiply by pi. PLEASE HELP!!!thanks in advance,Adam> Date: Wed, 23 May 2007 18:08:20 +0200> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> Subject: Re: [Tutor] trouble with "if"> CC: tutor@python.org> > The problem is with types. The outcome of raw_input is a string. But> if you give the line:> > if shape == 1:> > you are comparing it with a number. The text "1" is not equal to the> number 1, so this evaluates to False.> > Instead you should do:> > if shape == "1":> > To also be able to type 'circle' instead of '1', you can do:> > if shape == "1" or shape == "circle":> > or alternatively:> > if shape in ["1","circle"]:> > > > Andre Engels> > 2007/5/23, adam urbas <[EMAIL PROTECTED]>:> >> > Hi all,> >> > I've been working with this new program that I wrote. I started out with it> > on a Ti-83, which is much easier to program than python. Now I'm trying to> > transfer the program to python but its proving to be quite difficult. I'm> > not sure what the whole indentation thing is for. And now I'm having> > trouble with the if statement things.> >> > #"Circle Data Calculation Program:"> > print "Welcome to the Circle Data Calcuation Program."> > print> >> > #"Menu 1:"> > print "Pick a shape:"> > print "(NOTE: You must select the number of the shape and not the shape> > itself)"> > print "1 Circle"> > print "2 Square"> > print "3 Triangle"> >> > #"User's Choice:"> > shape=raw_input("> ")> >> > #"Select Given:"> > if shape == 1:> > print "Choose the given value:"> > print "1 radius"> > print "2 diameter"> > print "3 circumference"> > print "4 area"> >> > #"User's Choice:"> > given=raw_input("> ")> >> > if given == 1:> > radius=raw_input("Enter Radius:")> > diameter=(radius*2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Diameter:", diameter> > print "Circumference:", circumference> > print "Area:", area> >> > if given == 2:> > diameter=raw_input("Enter Diameter:")> > radius=(diameter/2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Circumference:", circumference> > print "Area:", area> >> > if given == 3:> > circumference=raw_input("Enter Circumference:")> > radius=(circumference/3.14/2)> > diameter=(radius*2)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Diameter:", diameter> > print "Area:", area> >> > if given == 4:> > area=raw_input("Enter Area:")> > radius=(area/3.14)> >> > This is the whole program so far, because I haven't quite finished it yet.> > But I tried to get it to display another list of options after you select a> > shape but it just does this.> >> > Pick a shape:> > 1 Circle> > 2 Square> > 3 Triangle> > >1> > >1> > >>>> >> > I'm not sure why it does that but I do know that it is skipping the second> > list of options.> >> > Another of my problems is that I can't figure out how to get it to accept> > two different inputs for a selection. Like I want it to accept both the> > number 1 and circle as circle then list the options for circle. It won't> > even accept words. I can only get it to accept numbers. It's quite> > frustrating actually.> >> > Any advice would be greatly appreciated.> > Thanks in advance,> > Adam> >> >> >> >> >> > I tried to get it to display ano> >> > > > Add some color. Personalize your inbox with your favorite colors. Try it!> > ___> > Tutor maillist - Tutor@python.org> > http://mail.python.org/mailman/listinfo/tutor> >> >> > > -- > Andre Engels, [EMAIL PROTECTED]> ICQ: 6260644 -- Skype: a_engels _ Add some color. Personalize your inbox with your favorite colors. www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_addcolor_0507___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
Sorry, I forgot to attach the files. Don't critique too much. If you find a mistake in the program, then I probably haven't gotten that far, since it isn't complete yet. I'm pretty much on the editing phase now.> Date: Wed, 23 May 2007 18:08:20 +0200> From: [EMAIL PROTECTED]> To: [EMAIL PROTECTED]> Subject: Re: [Tutor] trouble with "if"> CC: tutor@python.org> > The problem is with types. The outcome of raw_input is a string. But> if you give the line:> > if shape == 1:> > you are comparing it with a number. The text "1" is not equal to the> number 1, so this evaluates to False.> > Instead you should do:> > if shape == "1":> > To also be able to type 'circle' instead of '1', you can do:> > if shape == "1" or shape == "circle":> > or alternatively:> > if shape in ["1","circle"]:> > > > Andre Engels> > 2007/5/23, adam urbas <[EMAIL PROTECTED]>:> >> > Hi all,> >> > I've been working with this new program that I wrote. I started out with it> > on a Ti-83, which is much easier to program than python. Now I'm trying to> > transfer the program to python but its proving to be quite difficult. I'm> > not sure what the whole indentation thing is for. And now I'm having> > trouble with the if statement things.> >> > #"Circle Data Calculation Program:"> > print "Welcome to the Circle Data Calcuation Program."> > print> >> > #"Menu 1:"> > print "Pick a shape:"> > print "(NOTE: You must select the number of the shape and not the shape> > itself)"> > print "1 Circle"> > print "2 Square"> > print "3 Triangle"> >> > #"User's Choice:"> > shape=raw_input("> ")> >> > #"Select Given:"> > if shape == 1:> > print "Choose the given value:"> > print "1 radius"> > print "2 diameter"> > print "3 circumference"> > print "4 area"> >> > #"User's Choice:"> > given=raw_input("> ")> >> > if given == 1:> > radius=raw_input("Enter Radius:")> > diameter=(radius*2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Diameter:", diameter> > print "Circumference:", circumference> > print "Area:", area> >> > if given == 2:> > diameter=raw_input("Enter Diameter:")> > radius=(diameter/2)> > circumference=(diameter*3.14)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Circumference:", circumference> > print "Area:", area> >> > if given == 3:> > circumference=raw_input("Enter Circumference:")> > radius=(circumference/3.14/2)> > diameter=(radius*2)> > area=(radius**2*3.14)> > print "Radius:", radius> > print "Diameter:", diameter> > print "Area:", area> >> > if given == 4:> > area=raw_input("Enter Area:")> > radius=(area/3.14)> >> > This is the whole program so far, because I haven't quite finished it yet.> > But I tried to get it to display another list of options after you select a> > shape but it just does this.> >> > Pick a shape:> > 1 Circle> > 2 Square> > 3 Triangle> > >1> > >1> > >>>> >> > I'm not sure why it does that but I do know that it is skipping the second> > list of options.> >> > Another of my problems is that I can't figure out how to get it to accept> > two different inputs for a selection. Like I want it to accept both the> > number 1 and circle as circle then list the options for circle. It won't> > even accept words. I can only get it to accept numbers. It's quite> > frustrating actually.> >> > Any advice would be greatly appreciated.> > Thanks in advance,> > Adam> >> >> >> >> >> > I tried to get it to display ano> >> > > > Add some color. Personalize your inbox with your favorite colors. Try it!> > __
Re: [Tutor] trouble with "if"
adam urbas wrote: > Hi all, > > I've been working with this new program that I wrote. ... > #"User's Choice:" > shape=raw_input("> ") > > #"Select Given:" > if shape == 1: ... [EMAIL PROTECTED]:~$ python Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> shape = raw_input('>') >1 >>> type(shape) >>> shape == 1 False >>> HTH, Eric. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
adam urbas said unto the world upon 05/23/2007 11:57 AM: > > Hi all, > > I've been working with this new program that I wrote. I started out > with it on a Ti-83, which is much easier to program than python. Now > I'm trying to transfer the program to python but its proving to be quite > difficult. I'm not sure what the whole indentation thing is for. And > now I'm having trouble with the if statement things. > > #"Circle Data Calculation Program:" > print "Welcome to the Circle Data Calcuation Program." > print > > #"Menu 1:" > print "Pick a shape:" > print "(NOTE: You must select the number of the shape and not the shape > itself)" > print "1 Circle" > print "2 Square" > print "3 Triangle" > > #"User's Choice:" > shape=raw_input("> ") > > #"Select Given:" > if shape == 1: > print "Choose the given value:" > print "1 radius" > print "2 diameter" > print "3 circumference" > print "4 area" > > #"User's Choice:" > given=raw_input("> ") > > if given == 1: > radius=raw_input("Enter Radius:") > diameter=(radius*2) > circumference=(diameter*3.14) > area=(radius**2*3.14) > print "Diameter:", diameter > print "Circumference:", circumference > print "Area:", area > > if given == 2: > diameter=raw_input("Enter Diameter:") > radius=(diameter/2) > circumference=(diameter*3.14) > area=(radius**2*3.14) > print "Radius:", radius > print "Circumference:", circumference > print "Area:", area > > if given == 3: > circumference=raw_input("Enter Circumference:") > radius=(circumference/3.14/2) > diameter=(radius*2) > area=(radius**2*3.14) > print "Radius:", radius > print "Diameter:", diameter > print "Area:", area > > if given == 4: > area=raw_input("Enter Area:") > radius=(area/3.14) > > This is the whole program so far, because I haven't quite finished it > yet. But I tried to get it to display another list of options after you > select a shape but it just does this. > > Pick a shape: > 1 Circle > 2 Square > 3 Triangle > >1 > >1 > >>> > > I'm not sure why it does that but I do know that it is skipping the > second list of options. > > Another of my problems is that I can't figure out how to get it to > accept two different inputs for a selection. Like I want it to accept > both the number 1 and circle as circle then list the options for > circle. It won't even accept words. I can only get it to accept > numbers. It's quite frustrating actually. > > Any advice would be greatly appreciated. > Thanks in advance, > Adam > > Adam, Could you send plain text email rather than html, please? At least for me, your code's indentation is all messed up unless I take some steps to rectify it. The problem is that raw_input returns a string, and you are testing whether given is equal to integers. See if this helps make things clear: >>> data = raw_input('Feed me!') Feed me!42 >>> type(data) >>> data == 42 False >>> int(data) == 42 True >>> Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with "if"
The problem is with types. The outcome of raw_input is a string. But if you give the line: if shape == 1: you are comparing it with a number. The text "1" is not equal to the number 1, so this evaluates to False. Instead you should do: if shape == "1": To also be able to type 'circle' instead of '1', you can do: if shape == "1" or shape == "circle": or alternatively: if shape in ["1","circle"]: Andre Engels 2007/5/23, adam urbas <[EMAIL PROTECTED]>: > > Hi all, > > I've been working with this new program that I wrote. I started out with it > on a Ti-83, which is much easier to program than python. Now I'm trying to > transfer the program to python but its proving to be quite difficult. I'm > not sure what the whole indentation thing is for. And now I'm having > trouble with the if statement things. > > #"Circle Data Calculation Program:" > print "Welcome to the Circle Data Calcuation Program." > print > > #"Menu 1:" > print "Pick a shape:" > print "(NOTE: You must select the number of the shape and not the shape > itself)" > print "1 Circle" > print "2 Square" > print "3 Triangle" > > #"User's Choice:" > shape=raw_input("> ") > > #"Select Given:" > if shape == 1: > print "Choose the given value:" > print "1 radius" > print "2 diameter" > print "3 circumference" > print "4 area" > > #"User's Choice:" > given=raw_input("> ") > > if given == 1: > radius=raw_input("Enter Radius:") > diameter=(radius*2) > circumference=(diameter*3.14) > area=(radius**2*3.14) > print "Diameter:", diameter > print "Circumference:", circumference > print "Area:", area > > if given == 2: > diameter=raw_input("Enter Diameter:") > radius=(diameter/2) > circumference=(diameter*3.14) > area=(radius**2*3.14) > print "Radius:", radius > print "Circumference:", circumference > print "Area:", area > > if given == 3: > circumference=raw_input("Enter Circumference:") > radius=(circumference/3.14/2) > diameter=(radius*2) > area=(radius**2*3.14) > print "Radius:", radius > print "Diameter:", diameter > print "Area:", area > > if given == 4: > area=raw_input("Enter Area:") > radius=(area/3.14) > > This is the whole program so far, because I haven't quite finished it yet. > But I tried to get it to display another list of options after you select a > shape but it just does this. > > Pick a shape: > 1 Circle > 2 Square > 3 Triangle > >1 > >1 > >>> > > I'm not sure why it does that but I do know that it is skipping the second > list of options. > > Another of my problems is that I can't figure out how to get it to accept > two different inputs for a selection. Like I want it to accept both the > number 1 and circle as circle then list the options for circle. It won't > even accept words. I can only get it to accept numbers. It's quite > frustrating actually. > > Any advice would be greatly appreciated. > Thanks in advance, > Adam > > > > > > I tried to get it to display ano > > > Add some color. Personalize your inbox with your favorite colors. Try it! > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Andre Engels, [EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor