Python Code help For Implement a list

SL MULTIMEDIA TUTORIAL

Well-known member
  • Jul 27, 2020
    1,632
    6,937
    113
    United States
    meka karaganna Code eka kohomada wenas karanna one :-)👀

    Objectives

    • Extend the functionali of the the calculator to save and display the past results of the arithmetic operations
    Save and display calculation history of the calculator

    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 '?'.
    Task 1: Study the given code in the answer box and extend it to save each executed operation in a Python 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
    Task 2: implement a history() function to handle the operation '?'
    • 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"


    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()

    history āļšāˇ’āļēāļŊāˇ List Define āļšāļģāļŊāˇ result ekata append āļšāļģāˇ āļąāļ¸āˇŠ ⎄āļģ⎒ āļšāˇ’āļēāļŊāˇ ⎄⎒āļ­āļŊāˇ try āļšāļģāļŊāˇ āļļ⎐āļŊāˇ”āˇ€āˇ ⎄āļģ⎒ āļœāˇ’āļē⎚ āļąāˇāˇ„⎐.:(:sorry:
     
    historyData = []

    def history():
    print("Previous Operations : ")
    if(len(historyData) == 0):
    print("No past calculations to show ")
    else:
    for h in historyData:
    print(h)



    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 == '?'):
    history()
    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 )
    historyData.append(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()
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,632
    6,937
    113
    United States
    historyData = []

    def history():
    print("Previous Operations : ")
    if(len(historyData) == 0):
    print("No past calculations to show ")
    else:
    for h in historyData:
    print(h)



    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 == '?'):
    history()
    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 )
    historyData.append(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()
    Indentation ekka Code eka danna Puluwanda :-)
     
    • Like
    Reactions: johnferna678
    meka karaganna Code eka kohomada wenas karanna one :-)👀

    Objectives

    • Extend the functionali of the the calculator to save and display the past results of the arithmetic operations
    Save and display calculation history of the calculator

    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 '?'.
    Task 1: Study the given code in the answer box and extend it to save each executed operation in a Python 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
    Task 2: implement a history() function to handle the operation '?'
    • 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"


    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()

    history āļšāˇ’āļēāļŊāˇ List Define āļšāļģāļŊāˇ result ekata append āļšāļģāˇ āļąāļ¸āˇŠ ⎄āļģ⎒ āļšāˇ’āļēāļŊāˇ ⎄⎒āļ­āļŊāˇ try āļšāļģāļŊāˇ āļļ⎐āļŊāˇ”āˇ€āˇ ⎄āļģ⎒ āļœāˇ’āļē⎚ āļąāˇāˇ„⎐.:(:sorry:

    Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): + + Enter first number: 2 2 Enter second number: 2 2 2.0 + 2.0 = 4.0

    meka wada karanawa ne mokdda aula?

    āļ”āļē UOM āļ‘āļšāˇš online python course āļ‘āļšāļ§ āļąāˇšāļ¯?
    āļ‹āļąāˇŠāļœāˇ™ āļ¸āˇœāļšāļšāˇŠ ⎄āļģ⎒ āļ…⎀⎔āļŊāļšāˇŠ āļ­āˇ’āļē⎙āļąāˇ€ āļļāļąāˇŠ. āļ¸āļ¸āļ­āˇŠ āļ•āļš āˇ„āļģ⎒āļēāļ§ āļœāˇāˇ„⎔⎀āļ§ āˇ€āˇāļģāļ¯āˇ’āļē⎒ āļšāˇ’āļēāļąāˇ€āˇ. ⎀⎙āļą āļ’⎀āļŊ run ⎀⎙āļąāˇ€āˇ. :oo:
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,632
    6,937
    113
    United States
    āļ”āļē UOM āļ‘āļšāˇš online python course āļ‘āļšāļ§ āļąāˇšāļ¯?
    āļ‹āļąāˇŠāļœāˇ™ āļ¸āˇœāļšāļšāˇŠ ⎄āļģ⎒ āļ…⎀⎔āļŊāļšāˇŠ āļ­āˇ’āļē⎙āļąāˇ€ āļļāļąāˇŠ. āļ¸āļ¸āļ­āˇŠ āļ•āļš āˇ„āļģ⎒āļēāļ§ āļœāˇāˇ„⎔⎀āļ§ āˇ€āˇāļģāļ¯āˇ’āļē⎒ āļšāˇ’āļēāļąāˇ€āˇ. ⎀⎙āļą āļ’⎀āļŊ run ⎀⎙āļąāˇ€āˇ. :oo:
    ow
    meka kare nadda 👀
     

    hancok

    Well-known member
  • Oya part 2 da?
    Eke part 1 eken issarahata yanna baruwa inne. Monawa wenas karala gahuwath weradiylu. :oo:

    Ubata part 1 eka hari giya nan e code eka methana dapanko
    stock apu code kalla go kiyala function ekak asse danna one na. mama nikan damme passe recall karanna ona unoth kiyala

    Python:
    def select_op(choice):
        lst = ["","+","-","*","/","^","%"]
        if choice == "#":
            return 7
        elif choice in lst:
            return lst.index(choice)
    
        # take input from the user
    def inputfn():
        choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
       
        print(choice)
        choicenum = select_op(choice)
       
        if(choicenum == 7):
        #program ends here
            print("Done. Terminating")
            exit()
        elif "$" in choice:
            inputfn()
        elif choicenum in range(1,7):
            number1 = input("Enter first number: ")
            print(number1)
            if "$" in number1:
                go()
            if "#" in number1:
                print("Done. Terminating")
                exit()
            number2 = input("Enter second number: ")
            print(number2)
            if "$" in number2:
                go()
            if "#" in number2:
                print("Done. Terminating")
                exit()
           
            if choicenum == 1:
                print(float(number1),"+",float(number2),"=",float(number1) + float(number2))
               
            elif choicenum == 2:
                print(float(number1),"+",float(number2),"=",float(number1) - float(number2))
               
            elif choicenum == 3:
                print(float(number1),"+",float(number2),"=",float(number1) * float(number2))
               
            elif choicenum == 4:
                if number2 == "0":
                    print("float division by zero")
                    print(float(number1),"/",float(number2),"=","None")
                else:
                    print(float(number1),"+",float(number2),"=",float(number1) / float(number2))
               
            elif choicenum == 5:
                print(float(number1),"+",float(number2),"=",float(number1) ** float(number2))
               
            elif choicenum == 6:
                print(float(number1),"+",float(number2),"=",float(number1) % float(number2))
               
               
    def go():
        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    : $ ")
            inputfn()
    
    go()


    ah na na go() kiyana eka define karanna one. madin recall wela neh
     
    Last edited:
    • Like
    Reactions: shenat

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,632
    6,937
    113
    United States
    UOM āļ‘āļšāˇš online python course eka Complete karapu aya nadda :-) 👀

    stock apu code kalla go kiyala function ekak asse danna one na. mama nikan damme passe recall karanna ona unoth kiyala

    Python:
    def select_op(choice):
        lst = ["","+","-","*","/","^","%"]
        if choice == "#":
            return 7
        elif choice in lst:
            return lst.index(choice)
    
        # take input from the user
    def inputfn():
        choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
     
        print(choice)
        choicenum = select_op(choice)
     
        if(choicenum == 7):
        #program ends here
            print("Done. Terminating")
            exit()
        elif "$" in choice:
            inputfn()
        elif choicenum in range(1,7):
            number1 = input("Enter first number: ")
            print(number1)
            if "$" in number1:
                go()
            if "#" in number1:
                print("Done. Terminating")
                exit()
            number2 = input("Enter second number: ")
            print(number2)
            if "$" in number2:
                go()
            if "#" in number2:
                print("Done. Terminating")
                exit()
         
            if choicenum == 1:
                print(float(number1),"+",float(number2),"=",float(number1) + float(number2))
             
            elif choicenum == 2:
                print(float(number1),"+",float(number2),"=",float(number1) - float(number2))
             
            elif choicenum == 3:
                print(float(number1),"+",float(number2),"=",float(number1) * float(number2))
             
            elif choicenum == 4:
                if number2 == "0":
                    print("float division by zero")
                    print(float(number1),"/",float(number2),"=","None")
                else:
                    print(float(number1),"+",float(number2),"=",float(number1) / float(number2))
             
            elif choicenum == 5:
                print(float(number1),"+",float(number2),"=",float(number1) ** float(number2))
             
            elif choicenum == 6:
                print(float(number1),"+",float(number2),"=",float(number1) % float(number2))
             
             
    def go():
        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    : $ "
            inputfn()
    
    go()


    ah na na go() kiyana eka define karanna one. madin recall wela neh
    Mama kiyapu widhata str karala ekathu karala baluwa hari giye naha
    ------ Post added on Mar 15, 2022 at 10:20 AM
     
    Last edited:
    UOM āļ‘āļšāˇš online python course eka Complete karapu aya nadda :-) 👀


    Mama kiyapu widhata str karala ekathu karala baluwa hari giye naha
    ------ Post added on Mar 15, 2022 at 10:20 AM
    mage student kenektatah oka wela thibba baladdi answer eka print karapu nisa ehema wela thibbe question eke instructions hariyata kiyawanna samaharawita answer eka return karannada dan na thiyenne.

    Indentation ekka Code eka danna Puluwanda :-)
    Ah sorry mn danmm
    ------ Post added on Mar 15, 2022 at 11:15 AM
     
    Python:
    historyData = []
    def history():
      print("Previous Operations : ")
      if(len(historyData) == 0):
          print("No past calculations to show ")
      else:
          for h in historyData:
            print(h)
     
     
    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 == '?'):
        history()
      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 )
        historyData.append(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()
     
    stock apu code kalla go kiyala function ekak asse danna one na. mama nikan damme passe recall karanna ona unoth kiyala

    Python:
    def select_op(choice):
        lst = ["","+","-","*","/","^","%"]
        if choice == "#":
            return 7
        elif choice in lst:
            return lst.index(choice)
    
        # take input from the user
    def inputfn():
        choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
     
        print(choice)
        choicenum = select_op(choice)
     
        if(choicenum == 7):
        #program ends here
            print("Done. Terminating")
            exit()
        elif "$" in choice:
            inputfn()
        elif choicenum in range(1,7):
            number1 = input("Enter first number: ")
            print(number1)
            if "$" in number1:
                go()
            if "#" in number1:
                print("Done. Terminating")
                exit()
            number2 = input("Enter second number: ")
            print(number2)
            if "$" in number2:
                go()
            if "#" in number2:
                print("Done. Terminating")
                exit()
         
            if choicenum == 1:
                print(float(number1),"+",float(number2),"=",float(number1) + float(number2))
             
            elif choicenum == 2:
                print(float(number1),"+",float(number2),"=",float(number1) - float(number2))
             
            elif choicenum == 3:
                print(float(number1),"+",float(number2),"=",float(number1) * float(number2))
             
            elif choicenum == 4:
                if number2 == "0":
                    print("float division by zero")
                    print(float(number1),"/",float(number2),"=","None")
                else:
                    print(float(number1),"+",float(number2),"=",float(number1) / float(number2))
             
            elif choicenum == 5:
                print(float(number1),"+",float(number2),"=",float(number1) ** float(number2))
             
            elif choicenum == 6:
                print(float(number1),"+",float(number2),"=",float(number1) % float(number2))
             
             
    def go():
        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    : $ ")
            inputfn()
    
    go()


    ah na na go() kiyana eka define karanna one. madin recall wela neh

    āļ’āļšāļ­āˇŠ āļšāļģāˇ āļļāļąāˇŠ.
    āļ¯āˇāļąāˇŠ āļ¸āˇš āļ‹āļļ⎚ āļ‘āļš āļļāļŊāļŊ ⎀⎐āļģ⎐āļ¯āˇŠāļ¯ āˇ„āˇœāļēāˇāļœāļ­āˇŠāļ­āˇ™. operation āļ‘āļšāļšāˇŠ number āļ‘āļšāļšāˇŠ input āļ¯āˇ™āļą āˇ„āˇāļ¸āˇ€āˇ™āļŊ⎚āļ¸ āļ’āļšāļ§ print āļ‘āļšāļšāˇŠ āļ†āļ´āˇ„⎔ āļ¯āˇ™āļąāˇŠāļą āļ•āļą. āļ¸āˇœāļą āļœāˇœāļļ⎊āļļ ⎀⎐āļŠāļšāˇŠāļ¯ āļ’āļš. āļ…āļ´āˇ’ āļ¯āˇ™āļą input āļ‘āļš āļ´āˇšāļąāˇ€āļąāˇ™ āļ§āļē⎒āļ´āˇŠ āļšāļģāļąāļšāˇœāļ§. āļ†āļē⎙āļ­āˇŠ print āļšāļģāļąāˇŠāļąāˇ™ āļ¸āˇœāļšāļ§āļ¯. āļ‹āļąāˇŠāļ§ āļ´āˇ’āˇƒāˇŠāˇƒāˇ”āļ¯ āļ¸āļąāˇŠāļ¯ :oo:
     

    hancok

    Well-known member
  • āļ’āļšāļ­āˇŠ āļšāļģāˇ āļļāļąāˇŠ.
    āļ¯āˇāļąāˇŠ āļ¸āˇš āļ‹āļļ⎚ āļ‘āļš āļļāļŊāļŊ ⎀⎐āļģ⎐āļ¯āˇŠāļ¯ āˇ„āˇœāļēāˇāļœāļ­āˇŠāļ­āˇ™. operation āļ‘āļšāļšāˇŠ number āļ‘āļšāļšāˇŠ input āļ¯āˇ™āļą āˇ„āˇāļ¸āˇ€āˇ™āļŊ⎚āļ¸ āļ’āļšāļ§ print āļ‘āļšāļšāˇŠ āļ†āļ´āˇ„⎔ āļ¯āˇ™āļąāˇŠāļą āļ•āļą. āļ¸āˇœāļą āļœāˇœāļļ⎊āļļ ⎀⎐āļŠāļšāˇŠāļ¯ āļ’āļš. āļ…āļ´āˇ’ āļ¯āˇ™āļą input āļ‘āļš āļ´āˇšāļąāˇ€āļąāˇ™ āļ§āļē⎒āļ´āˇŠ āļšāļģāļąāļšāˇœāļ§. āļ†āļē⎙āļ­āˇŠ print āļšāļģāļąāˇŠāļąāˇ™ āļ¸āˇœāļšāļ§āļ¯. āļ‹āļąāˇŠāļ§ āļ´āˇ’āˇƒāˇŠāˇƒāˇ”āļ¯ āļ¸āļąāˇŠāļ¯ :oo:
    Print wenne na input deela enter ebuwata. Print denna one
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,632
    6,937
    113
    United States
    Python:
    historyData = []
    def history():
      print("Previous Operations : ")
      if(len(historyData) == 0):
          print("No past calculations to show ")
      else:
          for h in historyData:
            print(h)
     
     
    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 == '?'):
        history()
      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 )
        historyData.append(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()
    meka thibunama Error ekak awa eka ain kalata Passe Code eka hari giya :D
    Python:
    print("Previous Operations : ")

    ai

    harigiye natte. mokakda enne. debug karala balapan
    Hari bn wade karagaththa :-)
    ------ Post added on Mar 15, 2022 at 5:33 PM
     
    • Like
    Reactions: hancok

    -333-

    Well-known member
  • Oya part 2 da?
    Eke part 1 eken issarahata yanna baruwa inne. Monawa wenas karala gahuwath weradiylu. :oo:

    Ubata part 1 eka hari giya nan e code eka methana dapanko
    Menna meka thma Authors solution eka.

    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

    if choice == '+':
    print(num1, "+", num2, "=", add(num1, num2))

    elif choice == '-':
    print(num1, "-", num2, "=", subtract(num1, num2))

    elif choice == '*':
    print(num1, "*", num2, "=", multiply(num1, num2))

    elif choice == '/':
    print(num1, "/", num2, "=", divide(num1, num2))
    elif choice == '^':
    print(num1, "^", num2, "=", power(num1, num2))
    elif choice == '%':
    print(num1, "%", num2, "=", remainder(num1, num2))
    else:
    print("Something Went Wrong")
    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 : $ ")

    # take input from the user
    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)
    if(select_op(choice) == -1):
    #program ends here
    print("Done. Terminating")
    exit()

    meka thibunama Error ekak awa eka ain kalata Passe Code eka hari giya :D
    Python:
    print("Previous Operations : ")


    Hari bn wade karagaththa :-)
    ------ Post added on Mar 15, 2022 at 5:33 PM
    2nd Author Solution

    past_calculations =[];

    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 history():
    if past_calculations:
    for index,calc in enumerate(past_calculations):
    print(calc);
    else:
    print("No past calculations to show");
    return 0;

    def select_op(choice):
    if (choice == '?'):
    return history()
    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 )
    past_calculations.append(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()
    ------ Post added on Mar 17, 2022 at 12:37 PM