Python දන්න අය එන්නකෝ.

IT kolla

Active member
  • Mar 8, 2022
    172
    168
    43
    මේක මේ ම් dp education එකෙ python course 1 එකේ assignment 2 එක. මේකේ තියෙන්නේ history එක display කරන්න. මම ඒක හැදුවා. වැඩේ නම් හරියට වෙනවා. ඒත් මගේ history එකේ answer එක විතරයි පෙන්නන්නේ. මට ඔනේ සම්පූර්න පේලියම පෙන්නන්න. ඒ කිව්වෙ operators and operanands and the result. ඒත් මම කීප විදියකටම ට්‍ර්‍යි කලාට හරි ගියේ නෑ. මේක පොඩ්ඩක් බලන්න පුලුවන්ද? මම images and code එක දාලා තියෙනවා.

    elakiri 1.JPG


    elakri 2.JPG


    elakiri 3.JPG


    Python:
    last_calculation = []
    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  : ? ")
     
      def add(x,y):
          return x+y
    
      def substract(x,y):
          return x-y
    
      def multiply(x,y):
          return x*y
    
      def divide(x,y):
          return x/y
    
      def power(x,y):
          return x**y
    
      def remainder(x,y):
          return x%y
    
      def history():
        global last_calculation
        if last_calculation == []:
            print("No past calculations to show")
        else:
            for i in last_calculation:
                print(i)  #for single line
    
     
     
      # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        continue
      if choice=='?':
        history()
        continue
      try:
        a=input("Enter first number: ")
        print(a)
        if a == "#":
          #program ends here
          print("Done. Terminating")
          exit()
        a=float(a)
            
        b=input("Enter second number: ")
        print(b)
        if b == "#":
          #program ends here
          print("Done. Terminating")
          exit()
        b=float(b)
            
    
      except ValueError:
       continue
    
    
    
      if choice == "+":
          result = add(a, b)
          last_calculation.append(result)
          print(a, "+", b, "=", add(a,b))
          
    
      elif choice == "-":
          result = substract(a, b)
          last_calculation.append(result)
          print(a, "-", b, "=", substract(a,b))
    
      elif choice == "*":
          result = multiply(a, b)
          last_calculation.append(result)
          print(a, "*", b, "=", multiply(a,b))
    
      elif choice == "/":
          if b == 0.0:
              print("float division by zero")
              print(a, "/", b, "=" " None")
          else:
              result =  divide(a, b)
              last_calculation.append(result)
              print(a, "/", b, "=", divide(a,b))
          
      elif choice == '^':
          result = power(a, b)
          last_calculation.append(result)
          print(a, "^", b, "=", power(a,b))
    
      elif choice == "%":
          result = remainder(a, b)
          last_calculation.append(result)
          print(a, "%", b, "=", remainder(a,b))
    
      else :
          print("Unrecognized operation")
     

    tharakaf

    Well-known member
  • Oct 19, 2020
    36,203
    74,536
    113
    මේක මේ ම් dp education එකෙ python course 1 එකේ assignment 2 එක. මේකේ තියෙන්නේ history එක display කරන්න. මම ඒක හැදුවා. වැඩේ නම් හරියට වෙනවා. ඒත් මගේ history එකේ answer එක විතරයි පෙන්නන්නේ. මට ඔනේ සම්පූර්න පේලියම පෙන්නන්න. ඒ කිව්වෙ operators and operanands and the result. ඒත් මම කීප විදියකටම ට්‍ර්‍යි කලාට හරි ගියේ නෑ. මේක පොඩ්ඩක් බලන්න පුලුවන්ද? මම images and code එක දාලා තියෙනවා.

    View attachment 176311

    View attachment 176312

    View attachment 176313

    Python:
    last_calculation = []
    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  : ? ")
     
      def add(x,y):
          return x+y
    
      def substract(x,y):
          return x-y
    
      def multiply(x,y):
          return x*y
    
      def divide(x,y):
          return x/y
    
      def power(x,y):
          return x**y
    
      def remainder(x,y):
          return x%y
    
      def history():
        global last_calculation
        if last_calculation == []:
            print("No past calculations to show")
        else:
            for i in last_calculation:
                print(i)  #for single line
    
     
     
      # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        continue
      if choice=='?':
        history()
        continue
      try:
        a=input("Enter first number: ")
        print(a)
        if a == "#":
          #program ends here
          print("Done. Terminating")
          exit()
        a=float(a)
          
        b=input("Enter second number: ")
        print(b)
        if b == "#":
          #program ends here
          print("Done. Terminating")
          exit()
        b=float(b)
          
    
      except ValueError:
       continue
    
    
    
      if choice == "+":
          result = add(a, b)
          last_calculation.append(result)
          print(a, "+", b, "=", add(a,b))
        
    
      elif choice == "-":
          result = substract(a, b)
          last_calculation.append(result)
          print(a, "-", b, "=", substract(a,b))
    
      elif choice == "*":
          result = multiply(a, b)
          last_calculation.append(result)
          print(a, "*", b, "=", multiply(a,b))
    
      elif choice == "/":
          if b == 0.0:
              print("float division by zero")
              print(a, "/", b, "=" " None")
          else:
              result =  divide(a, b)
              last_calculation.append(result)
              print(a, "/", b, "=", divide(a,b))
        
      elif choice == '^':
          result = power(a, b)
          last_calculation.append(result)
          print(a, "^", b, "=", power(a,b))
    
      elif choice == "%":
          result = remainder(a, b)
          last_calculation.append(result)
          print(a, "%", b, "=", remainder(a,b))
    
      else :
          print("Unrecognized operation")
    Umba oka katat kaala taniyen goda dammoth tama umbata oka hondata puluwan wenne.

    Just saying :-)


    Answers dena unta --> Hints witarak deepan. Let him figure it out on his own. It will help him to learn (hope that is what he actually wants :-D )
     

    IT kolla

    Active member
  • Mar 8, 2022
    172
    168
    43
    enna one uttare mokakda?
    මට එන්න ඔනේ නම් මේ කොටු කරලා තියෙන එක. ඒත් මට එන්නෙ අනිත් පැත්තෙ විදියට අන්ස්වෙර් එක විතරයෛ.
    IMG-20220704-WA0001.jpg


    Umba oka katat kaala taniyen goda dammoth tama umbata oka hondata puluwan wenne.

    Just saying :-)


    Answers dena unta --> Hints witarak deepan. Let him figure it out on his own. It will help him to learn (hope that is what he actually wants :-D )
    ඔව් එහෙම හරි කමක් නෑ. මට ඔනේ මේක කරන ක්‍රමේ දැනගන්න. ට්‍ර්‍යි කරාට අවෙම නෑ. අනිත් ටික නම් හදා ගත්ත.
    ------ Post added on Jul 4, 2022 at 11:42 AM

    oke user dena hama input ekakma aimath output ekakwidiyata display wenna ona. elakiriye kalin thered ekak thibba oka gana.
    ඔව් මචන්. මේක නම් වෙනවා අවුලක් නැතුව. ඒත් අර සම්පූර්න line එකම එන්නෙ නැති එකයි අව්ල
    ------ Post added on Jul 4, 2022 at 11:42 AM
     
    • Like
    Reactions: tharakaf

    pasindu201

    Well-known member
  • Dec 25, 2017
    1,738
    1,524
    113
    mulinma balanna history function eka wada karanne kohomada? kohenda ekata value enne? ai a value eka ena thaning result eka witharak enne? apita a result eka hadena thanata wena mukuth danna barida?