making local variable work???? Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolindienc
    New Member
    • Aug 2006
    • 48

    making local variable work???? Help

    I am not sure what am I supposed to assign for my while loop to work at the bottom. Please help me to understand what I am doing wrong. Below is my code and I have highlighted my main module. I know that is where the problem is.

    Andy

    Code:
    from math import *
    
    def menu():
        print "\nSlect one of the following:"
        print "1. Calculate Area of Rectangle"
        print "2. Calculate Area of Circle"
        print "3. Quit"
        menuSel=input("Please enter your selection: ")
        while menuSel!=1 and menuSel!=2 and menuSel!=3:
            menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
            return menuSel
    
    def rectangle():
        print "You have chosen a rectangle."
        b=input("enter base  : ")
        h=input("enter height:")
        print "The area is ", b*h
        return
    
    def circle():
        print "You have chosen a circle."
        r=input("enter radius: ")
        print "The area is ",pi*r**2
        return
    
        
    [B]def main():
        choice = menu()
        while menuSel!=3:
            if menuSel==1:
                rectangle()
            elif menuSel==2:
                circle()
            menu()
        return[/B]
    
    main()
    raw_input("\nPress enter to exit.")
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Code:
    def main():
        choice = menu()
        while choice != 3:
            if choice == 1:
                rectangle()
            elif choice == 2:
                circle()
            choice = menu()
        return
    functions return values not names. so assign whatever name you want to the value of a fuction.
    Last edited by bartonc; Dec 9 '06, 01:15 AM. Reason: my bad

    Comment

    • fuffens
      New Member
      • Oct 2006
      • 38

      #3
      Code:
      from math import *
      
      def menu():
          print "\nSlect one of the following:"
          print "1. Calculate Area of Rectangle"
          print "2. Calculate Area of Circle"
          print "3. Quit"
          menuSel=input("Please enter your selection: ")
          while menuSel!=1 and menuSel!=2 and menuSel!=3:
              menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
          return menuSel
      
      def rectangle():
          print "You have chosen a rectangle."
          b=input("enter base  : ")
          h=input("enter height:")
          print "The area is ", b*h
          return
      
      def circle():
          print "You have chosen a circle."
          r=input("enter radius: ")
          print "The area is ",pi*r**2
          return
      
          
      def main():
          choice = menu()
          while choice!=3:
              if choice==1:
                  rectangle()
              elif choice==2:
                  circle()
              choice = menu()
          return
      
      main()
      raw_input("\nPress enter to exit.")
      I have updated your main function and menu function to do what I think you want to do. If the menu function returns a local variable named choice you must use that one, not the variable named menuSel from the menu function. In the menu function it was a simple tab fault. Only a correct menu selection should be returned.

      Best regards
      /Fredrik

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Andy,

        Here's another (not necessarily the best) way to use 'while' in menu():
        Code:
            while True:
                menuSel = input("Please enter your selection (1, 2, or 3): ")
                if menuSel in [1, 2, 3]:
                    break
                else:
                    print "Invalid Selection"
        Unless you need to return a value to the calling function, there is no need to use return:
        Code:
        def main():
            while True:
                choice = menu()
                if choice == 1:
                    rectangle()
                elif choice == 2:
                    circle()
                else:
                    break
        Function main returns 'None' with or without 'return'.

        Comment

        • coolindienc
          New Member
          • Aug 2006
          • 48

          #5
          Fredrik,
          That is exactly what I did at first, but as a result I started repeating menu and not actually going for any calculations based on my choice. I will not be able to quit either. I can only go further if I entered wrong choice and I have to enter my choice again from the menu.

          Any suggestions why it does so?

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by coolindienc
            Fredrik,
            That is exactly what I did at first, but as a result I started repeating menu and not actually going for any calculations based on my choice. I will not be able to quit either. I can only go further if I entered wrong choice and I have to enter my choice again from the menu.

            Any suggestions why it does so?
            look at main(). You weren't assigning a new "choice".

            Comment

            • coolindienc
              New Member
              • Aug 2006
              • 48

              #7
              bartonc,
              I did so before just like fredrik said even before I posted the discussion and it was not working. OK here is my copy of old one before I posted the earlier one. Below program does not recognize any of my choices. It keeps giving me menu again as a result.

              Andy

              Code:
              from math import *
              
              def menu():
                  print "\nSlect one of the following:"
                  print "1. Calculate Area of Rectangle"
                  print "2. Calculate Area of Circle"
                  print "3. Quit"
                  menuSel=input("Please enter your selection: ")
                  while menuSel!=1 and menuSel!=2 and menuSel!=3:
                      menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
                      return menuSel
              
              def rectangle():
                  print "You have chosen a rectangle."
                  b=input("enter base  : ")
                  h=input("enter height:")
                  print "The area is ", b*h
                  return
              
              def circle():
                  print "You have chosen a circle."
                  r=input("enter radius: ")
                  print "The area is ",pi*r**2
                  return
              
                  
              def main():
                  choice = menu()
                  while choice!=3:
                      if choice==1:
                          rectangle()
                      elif choice==2:
                          circle()
                      choice = menu()
                  return

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Then I don't know what you problem is. After de-indenting return in menu(), it works for me...


                Code:
                from math import *
                
                def menu():
                    print "\nSlect one of the following:"
                    print "1. Calculate Area of Rectangle"
                    print "2. Calculate Area of Circle"
                    print "3. Quit"
                    menuSel=input("Please enter your selection: ")
                    while menuSel!=1 and menuSel!=2 and menuSel!=3:
                        menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
                    return menuSel
                
                def rectangle():
                    print "You have chosen a rectangle."
                    b=input("enter base  : ")
                    h=input("enter height:")
                    print "The area is ", b*h
                
                def circle():
                    print "You have chosen a circle."
                    r=input("enter radius: ")
                    print "The area is ",pi*r**2
                
                    
                def main():
                    choice = menu()
                    while choice!=3:
                        if choice==1:
                            rectangle()
                        elif choice==2:
                            circle()
                        choice = menu()
                
                main()

                Comment

                Working...