Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Talk!
Python Code help For Implement a list
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="SL MULTIMEDIA TUTORIAL" data-source="post: 27465798" data-attributes="member: 575597"><p>meka karaganna Code eka kohomada wenas karanna one <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":-)" title="Smile :-)" loading="lazy" data-shortname=":-)" /><img class="smilie smilie--emoji" loading="lazy" alt="👀" title="Eyes :eyes:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f440.png" data-shortname=":eyes:" /></p><p></p><p><strong>Objectives</strong></p><p></p><ul> <li data-xf-list-type="ul">Extend the functionali of the the calculator to save and display the past results of the arithmetic operations</li> </ul><p><strong>Save and display calculation history of the calculator</strong></p><p></p><p>In this second stage of the assignment you will extend the given calculator program to record the calculations, and recall them as a list using an additional command '?'.</p><p><strong>Task 1: </strong>Study the given code in the answer box and extend it to save each executed operation in a Python List.</p><ul> <li data-xf-list-type="ul">Declare a list to store the previous operations</li> <li data-xf-list-type="ul">Save the operator, operands and the results as a single string, for each operation after each calculation</li> </ul><p><strong>Task 2: </strong>implement a history() function to handle the operation '?'</p><ul> <li data-xf-list-type="ul">Display the complete saved list of operations (in the order of execution) using a new command ‘?’</li> <li data-xf-list-type="ul">If there are no previous calculations when the history '?' command is used, you can display the following message "No past calculations to show"</li> </ul><p></p><p></p><p>[CODE=python]def add(a,b):</p><p> return a+b</p><p> </p><p>def subtract(a,b):</p><p> return a-b</p><p> </p><p>def multiply (a,b):</p><p> return a*b</p><p></p><p>def divide(a,b):</p><p> try:</p><p> return a/b</p><p> except Exception as e:</p><p> print(e)</p><p>def power(a,b):</p><p> return a**b</p><p> </p><p>def remainder(a,b):</p><p> return a%b</p><p> </p><p>def select_op(choice):</p><p> if (choice == '#'):</p><p> return -1</p><p> elif (choice == '$'):</p><p> return 0</p><p> elif (choice in ('+','-','*','/','^','%')):</p><p> while (True):</p><p> num1s = str(input("Enter first number: "))</p><p> print(num1s)</p><p> if num1s.endswith('$'):</p><p> return 0</p><p> if num1s.endswith('#'):</p><p> return -1</p><p> </p><p> try:</p><p> num1 = float(num1s)</p><p> break</p><p> except:</p><p> print("Not a valid number,please enter again")</p><p> continue</p><p> </p><p> while (True):</p><p> num2s = str(input("Enter second number: "))</p><p> print(num2s)</p><p> if num2s.endswith('$'):</p><p> return 0</p><p> if num2s.endswith('#'):</p><p> return -1</p><p> try:</p><p> num2 = float(num2s)</p><p> break</p><p> except:</p><p> print("Not a valid number,please enter again")</p><p> continue</p><p> </p><p> result = 0.0</p><p> last_calculation = ""</p><p> if choice == '+':</p><p> result = add(num1, num2)</p><p> elif choice == '-':</p><p> result = subtract(num1, num2)</p><p> elif choice == '*':</p><p> result = multiply(num1, num2)</p><p> elif choice == '/':</p><p> result = divide(num1, num2)</p><p> elif choice == '^':</p><p> result = power(num1, num2)</p><p> elif choice == '%':</p><p> result = remainder(num1, num2)</p><p> else:</p><p> print("Something Went Wrong")</p><p> </p><p> last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result)</p><p> print(last_calculation )</p><p> </p><p> else:</p><p> print("Unrecognized operation")</p><p> </p><p>while True:</p><p> print("Select operation.")</p><p> print("1.Add : + ")</p><p> print("2.Subtract : - ")</p><p> print("3.Multiply : * ")</p><p> print("4.Divide : / ")</p><p> print("5.Power : ^ ")</p><p> print("6.Remainder: % ")</p><p> print("7.Terminate: # ")</p><p> print("8.Reset : $ ")</p><p> print("8.History : ? ")</p><p> </p><p> # take input from the user</p><p> choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")</p><p> print(choice)</p><p> if(select_op(choice) == -1):</p><p> #program ends here</p><p> print("Done. Terminating")</p><p> exit()[/CODE]</p><p></p><p>history කියලා List Define කරලා result ekata append කරා නම් හරි කියලා හිතලා try කරලා බැලුවා හරි ගියේ නැහැ.<img src="/styles/default/xenforo/smilies/default/sad.gif" class="smilie" loading="lazy" alt=":(" title="Sad :(" data-shortname=":(" /><img src="/styles/default/xenforo/smilies/default/sorry.gif" class="smilie" loading="lazy" alt=":sorry:" title="Sorry :sorry:" data-shortname=":sorry:" /></p></blockquote><p></p>
[QUOTE="SL MULTIMEDIA TUTORIAL, post: 27465798, member: 575597"] meka karaganna Code eka kohomada wenas karanna one :-)👀 [B]Objectives[/B] [LIST] [*]Extend the functionali of the the calculator to save and display the past results of the arithmetic operations [/LIST] [B]Save and display calculation history of the calculator[/B] In this second stage of the assignment you will extend the given calculator program to record the calculations, and recall them as a list using an additional command '?'. [B]Task 1: [/B]Study the given code in the answer box and extend it to save each executed operation in a Python List. [LIST] [*]Declare a list to store the previous operations [*]Save the operator, operands and the results as a single string, for each operation after each calculation [/LIST] [B]Task 2: [/B]implement a history() function to handle the operation '?' [LIST] [*]Display the complete saved list of operations (in the order of execution) using a new command ‘?’ [*]If there are no previous calculations when the history '?' command is used, you can display the following message "No past calculations to show" [/LIST] [CODE=python]def add(a,b): return a+b def subtract(a,b): return a-b def multiply (a,b): return a*b def divide(a,b): try: return a/b except Exception as e: print(e) def power(a,b): return a**b def remainder(a,b): return a%b def select_op(choice): if (choice == '#'): return -1 elif (choice == '$'): return 0 elif (choice in ('+','-','*','/','^','%')): while (True): num1s = str(input("Enter first number: ")) print(num1s) if num1s.endswith('$'): return 0 if num1s.endswith('#'): return -1 try: num1 = float(num1s) break except: print("Not a valid number,please enter again") continue while (True): num2s = str(input("Enter second number: ")) print(num2s) if num2s.endswith('$'): return 0 if num2s.endswith('#'): return -1 try: num2 = float(num2s) break except: print("Not a valid number,please enter again") continue result = 0.0 last_calculation = "" if choice == '+': result = add(num1, num2) elif choice == '-': result = subtract(num1, num2) elif choice == '*': result = multiply(num1, num2) elif choice == '/': result = divide(num1, num2) elif choice == '^': result = power(num1, num2) elif choice == '%': result = remainder(num1, num2) else: print("Something Went Wrong") last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result) print(last_calculation ) else: print("Unrecognized operation") while True: print("Select operation.") print("1.Add : + ") print("2.Subtract : - ") print("3.Multiply : * ") print("4.Divide : / ") print("5.Power : ^ ") print("6.Remainder: % ") print("7.Terminate: # ") print("8.Reset : $ ") print("8.History : ? ") # take input from the user choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") print(choice) if(select_op(choice) == -1): #program ends here print("Done. Terminating") exit()[/CODE] history කියලා List Define කරලා result ekata append කරා නම් හරි කියලා හිතලා try කරලා බැලුවා හරි ගියේ නැහැ.:(:sorry: [/QUOTE]
Insert quotes…
Verification
Nawa warak dahaya keeyada? (Namaya wadi kireema dahaya)
Post reply
Top
Bottom