Question about recurssion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deathwing
    New Member
    • Mar 2007
    • 32

    #1

    Question about recurssion

    Hi everyone,

    I have just programmed a very very simple calculator program, I am actually rather pleased. I would just like to know how I can make it so that every time the user has entered their input and an output has been returned by the program, the program jumps back to the start and ask the user to choose another option. Here is the code for my very very simple calculator program.

    Code:
    # my first very simple calculator
    # this program only does basic arithmatic
    
    print\
        """
                                  #-------------#
                                  # MYCALC 1.0  #
                                  #-------------#
    
    WELCOME TO MYCALC 1.0 This is a command line program that you can use
    as a calculator just incase you don't have one installed on your
    desk top. Using it is pretty self explanitory.
        """
    print " You have the following options:\n"
    options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4',)
    
    print "\nHere are ur options:\n", options
    
    
    choice = int(raw_input("Plese enter your operation choice: "))
    
    if choice == 1:
        a = int(raw_input("enter value a: "))
        b = int(raw_input("enter value b: "))
        total = a+b
        print "\nThe total is:", total
    
    
    elif choice == 2:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a-b
        print "\nThe total is:", total
    
        
    elif choice == 3:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a/b
        print "\nThe total is:", total
      
    
    elif choice == 4:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a*b
        print "\nThe total is:", total
    
    else:
        print "\nYour choice is invalid"
    
            
    
    raw_input("\nPres enter to exit.")
    Thanks, -Deathwing
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Deathwing
    Hi everyone,

    I have just programmed a very very simple calculator program, I am actually rather pleased. I would just like to know how I can make it so that every time the user has entered their input and an output has been returned by the program, the program jumps back to the start and ask the user to choose another option. Here is the code for my very very simple calculator program.

    Code:
    # my first very simple calculator
    # this program only does basic arithmatic
    
    print\
        """
                                  #-------------#
                                  # MYCALC 1.0  #
                                  #-------------#
    
    WELCOME TO MYCALC 1.0 This is a command line program that you can use
    as a calculator just incase you don't have one installed on your
    desk top. Using it is pretty self explanitory.
        """
    print " You have the following options:\n"
    options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4',)
    
    print "\nHere are ur options:\n", options
    
    
    choice = int(raw_input("Plese enter your operation choice: "))
    
    if choice == 1:
        a = int(raw_input("enter value a: "))
        b = int(raw_input("enter value b: "))
        total = a+b
        print "\nThe total is:", total
    
    
    elif choice == 2:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a-b
        print "\nThe total is:", total
    
        
    elif choice == 3:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a/b
        print "\nThe total is:", total
      
    
    elif choice == 4:
        a = int(raw_input("\nenter value a: "))
        b = int(raw_input("\nenter value b: "))
        total = a*b
        print "\nThe total is:", total
    
    else:
        print "\nYour choice is invalid"
    
            
    
    raw_input("\nPres enter to exit.")
    Thanks, -Deathwing
    You do not need recursion - you need a while loop:
    Code:
    # my first very simple calculator
    # this program only does basic arithmatic
    
    print\
        """
                                  #-------------#
                                  # MYCALC 1.0  #
                                  #-------------#
    
    WELCOME TO MYCALC 1.0 This is a command line program that you can use
    as a calculator just incase you don't have one installed on your
    desk top. Using it is pretty self explanitory.
        """
    print " You have the following options:\n"
    options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4', 'exit = 5')
    
    print "\nHere are ur options:\n", options
    
    while True:
        choice = int(raw_input("Plese enter your operation choice: "))
    
        if choice == 1:
            a = int(raw_input("enter value a: "))
            b = int(raw_input("enter value b: "))
            total = a+b
            print "\nThe total is:", total
    
    
        elif choice == 2:
            a = int(raw_input("\nenter value a: "))
            b = int(raw_input("\nenter value b: "))
            total = a-b
            print "\nThe total is:", total
    
            
        elif choice == 3:
            a = int(raw_input("\nenter value a: "))
            b = int(raw_input("\nenter value b: "))
            total = a/b
            print "\nThe total is:", total
          
    
        elif choice == 4:
            a = int(raw_input("\nenter value a: "))
            b = int(raw_input("\nenter value b: "))
            total = a*b
            print "\nThe total is:", total
    
        elif choice == 5:
            break
    
        else:
            print "\nYour choice is invalid"
    
    raw_input("\nPress enter to exit.")
    If the user enters '5', the code 'breaks' out of the loop.

    Comment

    • Deathwing
      New Member
      • Mar 2007
      • 32

      #3
      Originally posted by bvdet
      You do not need recursion - you need a while loop:
      Code:
      # my first very simple calculator
      # this program only does basic arithmatic
      
      print\
          """
                                    #-------------#
                                    # MYCALC 1.0  #
                                    #-------------#
      
      WELCOME TO MYCALC 1.0 This is a command line program that you can use
      as a calculator just incase you don't have one installed on your
      desk top. Using it is pretty self explanitory.
          """
      print " You have the following options:\n"
      options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4', 'exit = 5')
      
      print "\nHere are ur options:\n", options
      
      while True:
          choice = int(raw_input("Plese enter your operation choice: "))
      
          if choice == 1:
              a = int(raw_input("enter value a: "))
              b = int(raw_input("enter value b: "))
              total = a+b
              print "\nThe total is:", total
      
      
          elif choice == 2:
              a = int(raw_input("\nenter value a: "))
              b = int(raw_input("\nenter value b: "))
              total = a-b
              print "\nThe total is:", total
      
              
          elif choice == 3:
              a = int(raw_input("\nenter value a: "))
              b = int(raw_input("\nenter value b: "))
              total = a/b
              print "\nThe total is:", total
            
      
          elif choice == 4:
              a = int(raw_input("\nenter value a: "))
              b = int(raw_input("\nenter value b: "))
              total = a*b
              print "\nThe total is:", total
      
          elif choice == 5:
              break
      
          else:
              print "\nYour choice is invalid"
      
      raw_input("\nPress enter to exit.")
      If the user enters '5', the code 'breaks' out of the loop.
      ah... ic ic okay, I'll take a look and see what happens gracias.

      Comment

      Working...