scalc.zip

How_to use_scalc.txt

step 0) go to www.pythontutor.com and test the code first to understand how it works here is the copy from the lesson def scalc(p): lstring=p.split(",") if lstring[2]=="*": res= int(lstring[0]) * int(lstring[1]) if lstring[2]=="+": res= int(lstring[0]) + int(lstring[1]) return res # test the function print(scalc("20,30,*")) # test using input() calc_string=input("Enter string in this format; value1, value2, operator") print(scalc(calc_string)) How to use/play/test scalc or any function in a library step 1) create two Python files for practice/testing a) for the library function(s) Test_lib.py b) for testing/using the function Test_lib_prg.py devlop / copy the code from the lesson to a library. Since this code is in the lesson I am copying into the library def calc(p): lstring=p.split(",") if lstring[2]=="*": res= int(lstring[0]) * int(lstring[1]) if lstring[2]=="+": res= int(lstring[0]) + int(lstring[1]) return res step 2) instrunction from the assignment scalc(p1) p1 will be a string like this "N1, N2, operator" examples scalc("20,30,*") the result will be 600 scalc("50,20,+") the result will be 70 scalc("50,20,-") the result will be 30 scalc("60,20,/") the result will be 30

Test_lib.py

def add(n1,n2): return n1+n2 def scalc(p): lstring=p.split(",") if lstring[2]=="*": res= int(lstring[0]) * int(lstring[1]) if lstring[2]=="+": #res= int(lstring[0]) + int(lstring[1]) res= add((lstring[0]), int(lstring[1])) return res

Test_lib_prg.py

import Test_lib as tl # if you do this all functions must be prefexed with tl after "as" # this is the prefered method for clarity """ or you can do this from Test_lib import scalc or this from Test_lib import * The above you do not have to prefex the library name """ # test the function print(tl.scalc("20,30,*")) # test using input() calc_string=input("Calc string in this format; value1, value2, operator -> ") print(tl.scalc(calc_string))

video1270503184.mp4

video1544364225.mp4