[Tutor] [Fwd: Re: trouble with "if"]

Brian van den Broek broek at cc.umanitoba.ca
Wed May 30 06:32:06 CEST 2007


Another fwd, folks.

Brian vdB

-------- Original Message --------
Subject: Re: [Tutor] trouble with "if"
Date: Tue, 29 May 2007 23:28:46 -0500
From: Adam Urbas <jped.aru at gmail.com>
To: Brian van den Broek <broek at cc.umanitoba.ca>
References: <BAY103-W11DE2E62FD41449A952D68B12F0 at phx.gbl>	 
<465C64A4.60602 at cc.umanitoba.ca>	 
<ef52818c0705292107n1ee0c105k54c02ccf4d85f7c6 at mail.gmail.com>

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 <jped.aru at gmail.com> 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 <broek at cc.umanitoba.ca> 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():
> >      # 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
> >
>




More information about the Tutor mailing list