473,414 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Question about recurssion

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.

Expand|Select|Wrap|Line Numbers
  1. # my first very simple calculator
  2. # this program only does basic arithmatic
  3.  
  4. print\
  5.     """
  6.                               #-------------#
  7.                               # MYCALC 1.0  #
  8.                               #-------------#
  9.  
  10. WELCOME TO MYCALC 1.0 This is a command line program that you can use
  11. as a calculator just incase you don't have one installed on your
  12. desk top. Using it is pretty self explanitory.
  13.     """
  14. print " You have the following options:\n"
  15. options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4',)
  16.  
  17. print "\nHere are ur options:\n", options
  18.  
  19.  
  20. choice = int(raw_input("Plese enter your operation choice: "))
  21.  
  22. if choice == 1:
  23.     a = int(raw_input("enter value a: "))
  24.     b = int(raw_input("enter value b: "))
  25.     total = a+b
  26.     print "\nThe total is:", total
  27.  
  28.  
  29. elif choice == 2:
  30.     a = int(raw_input("\nenter value a: "))
  31.     b = int(raw_input("\nenter value b: "))
  32.     total = a-b
  33.     print "\nThe total is:", total
  34.  
  35.  
  36. elif choice == 3:
  37.     a = int(raw_input("\nenter value a: "))
  38.     b = int(raw_input("\nenter value b: "))
  39.     total = a/b
  40.     print "\nThe total is:", total
  41.  
  42.  
  43. elif choice == 4:
  44.     a = int(raw_input("\nenter value a: "))
  45.     b = int(raw_input("\nenter value b: "))
  46.     total = a*b
  47.     print "\nThe total is:", total
  48.  
  49. else:
  50.     print "\nYour choice is invalid"
  51.  
  52.  
  53.  
  54. raw_input("\nPres enter to exit.")
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
Thanks, -Deathwing
Apr 2 '07 #1
2 1258
bvdet
2,851 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. # my first very simple calculator
  2. # this program only does basic arithmatic
  3.  
  4. print\
  5.     """
  6.                               #-------------#
  7.                               # MYCALC 1.0  #
  8.                               #-------------#
  9.  
  10. WELCOME TO MYCALC 1.0 This is a command line program that you can use
  11. as a calculator just incase you don't have one installed on your
  12. desk top. Using it is pretty self explanitory.
  13.     """
  14. print " You have the following options:\n"
  15. options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4',)
  16.  
  17. print "\nHere are ur options:\n", options
  18.  
  19.  
  20. choice = int(raw_input("Plese enter your operation choice: "))
  21.  
  22. if choice == 1:
  23.     a = int(raw_input("enter value a: "))
  24.     b = int(raw_input("enter value b: "))
  25.     total = a+b
  26.     print "\nThe total is:", total
  27.  
  28.  
  29. elif choice == 2:
  30.     a = int(raw_input("\nenter value a: "))
  31.     b = int(raw_input("\nenter value b: "))
  32.     total = a-b
  33.     print "\nThe total is:", total
  34.  
  35.  
  36. elif choice == 3:
  37.     a = int(raw_input("\nenter value a: "))
  38.     b = int(raw_input("\nenter value b: "))
  39.     total = a/b
  40.     print "\nThe total is:", total
  41.  
  42.  
  43. elif choice == 4:
  44.     a = int(raw_input("\nenter value a: "))
  45.     b = int(raw_input("\nenter value b: "))
  46.     total = a*b
  47.     print "\nThe total is:", total
  48.  
  49. else:
  50.     print "\nYour choice is invalid"
  51.  
  52.  
  53.  
  54. raw_input("\nPres enter to exit.")
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
Thanks, -Deathwing
You do not need recursion - you need a while loop:
Expand|Select|Wrap|Line Numbers
  1. # my first very simple calculator
  2. # this program only does basic arithmatic
  3.  
  4. print\
  5.     """
  6.                               #-------------#
  7.                               # MYCALC 1.0  #
  8.                               #-------------#
  9.  
  10. WELCOME TO MYCALC 1.0 This is a command line program that you can use
  11. as a calculator just incase you don't have one installed on your
  12. desk top. Using it is pretty self explanitory.
  13.     """
  14. print " You have the following options:\n"
  15. options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4', 'exit = 5')
  16.  
  17. print "\nHere are ur options:\n", options
  18.  
  19. while True:
  20.     choice = int(raw_input("Plese enter your operation choice: "))
  21.  
  22.     if choice == 1:
  23.         a = int(raw_input("enter value a: "))
  24.         b = int(raw_input("enter value b: "))
  25.         total = a+b
  26.         print "\nThe total is:", total
  27.  
  28.  
  29.     elif choice == 2:
  30.         a = int(raw_input("\nenter value a: "))
  31.         b = int(raw_input("\nenter value b: "))
  32.         total = a-b
  33.         print "\nThe total is:", total
  34.  
  35.  
  36.     elif choice == 3:
  37.         a = int(raw_input("\nenter value a: "))
  38.         b = int(raw_input("\nenter value b: "))
  39.         total = a/b
  40.         print "\nThe total is:", total
  41.  
  42.  
  43.     elif choice == 4:
  44.         a = int(raw_input("\nenter value a: "))
  45.         b = int(raw_input("\nenter value b: "))
  46.         total = a*b
  47.         print "\nThe total is:", total
  48.  
  49.     elif choice == 5:
  50.         break
  51.  
  52.     else:
  53.         print "\nYour choice is invalid"
  54.  
  55. raw_input("\nPress enter to exit.")
If the user enters '5', the code 'breaks' out of the loop.
Apr 2 '07 #2
You do not need recursion - you need a while loop:
Expand|Select|Wrap|Line Numbers
  1. # my first very simple calculator
  2. # this program only does basic arithmatic
  3.  
  4. print\
  5.     """
  6.                               #-------------#
  7.                               # MYCALC 1.0  #
  8.                               #-------------#
  9.  
  10. WELCOME TO MYCALC 1.0 This is a command line program that you can use
  11. as a calculator just incase you don't have one installed on your
  12. desk top. Using it is pretty self explanitory.
  13.     """
  14. print " You have the following options:\n"
  15. options = ('addition = 1','subtraction = 2', 'division = 3', 'multiplication = 4', 'exit = 5')
  16.  
  17. print "\nHere are ur options:\n", options
  18.  
  19. while True:
  20.     choice = int(raw_input("Plese enter your operation choice: "))
  21.  
  22.     if choice == 1:
  23.         a = int(raw_input("enter value a: "))
  24.         b = int(raw_input("enter value b: "))
  25.         total = a+b
  26.         print "\nThe total is:", total
  27.  
  28.  
  29.     elif choice == 2:
  30.         a = int(raw_input("\nenter value a: "))
  31.         b = int(raw_input("\nenter value b: "))
  32.         total = a-b
  33.         print "\nThe total is:", total
  34.  
  35.  
  36.     elif choice == 3:
  37.         a = int(raw_input("\nenter value a: "))
  38.         b = int(raw_input("\nenter value b: "))
  39.         total = a/b
  40.         print "\nThe total is:", total
  41.  
  42.  
  43.     elif choice == 4:
  44.         a = int(raw_input("\nenter value a: "))
  45.         b = int(raw_input("\nenter value b: "))
  46.         total = a*b
  47.         print "\nThe total is:", total
  48.  
  49.     elif choice == 5:
  50.         break
  51.  
  52.     else:
  53.         print "\nYour choice is invalid"
  54.  
  55. 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.
Apr 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
4
by: codefixer | last post by:
Hello: I am trying to understand why one of these crash while the other works fine. long recurssion(int t) { if(t <= 1) return(1); else return(t * recurssion(t--)); //crashes
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.