Uom python course 1 assignment part 1

olu bakka

Well-known member
  • Aug 18, 2011
    22,013
    22,421
    113
    Thank you for the answer.
    mama menna meka demmama hari giya kalin. eth dn EOFError kiyala ekk enwane.


    a=input("Enter first number: ")
    print(a)
    a=float(a)

    b=input("Enter second number: ")
    print(b)
    b=float(b)
    assignment 3.JPG
    Meke bn 2nd number input ekk program eken deela nane un. ekayi eof enne
    eth error ekk enna widhak nn na thama
     

    IT kolla

    Active member
  • Mar 8, 2022
    171
    168
    43
    Meke bn 2nd number input ekk program eken deela nane un. ekayi eof enne
    eth error ekk enna widhak nn na thama
    ow eth meke second number ekata # dunnoth program eka terminate wenna one hari nm. eth wela thiyenne aith program eka run wela. eka ehema wenne ai kiyala hoyagththa nm hari neda?
     

    olu bakka

    Well-known member
  • Aug 18, 2011
    22,013
    22,421
    113
    ow eth meke second number ekata # dunnoth program eka terminate wenna one hari nm. eth wela thiyenne aith program eka run wela. eka ehema wenne ai kiyala hoyagththa nm hari neda?
    එතන තමා මං හිතන්නෙ අවුල. උබේ code එකේ number එකකට # දුන්නට terminate වෙන්න ලියල නෑ. Choice එකේ # දුන්නොත් විතරයි terminate වෙන්නෙ.
     

    olu bakka

    Well-known member
  • Aug 18, 2011
    22,013
    22,421
    113
    Reset එකත් choice එකේ දැම්මොත් විතරයි වැඩ
    එතන තමා මං හිතන්නෙ අවුල. උබේ code එකේ number එකකට # දුන්නට terminate වෙන්න ලියල නෑ. Choice එකේ # දුන්නොත් විතරයි terminate වෙන්නෙ.
     

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    48,497
    98,447
    113
    Python:
    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 remainder(x, y): return x % y
    def power(x, y): return x**y
    
    func_list = (add, substract, multiply, divide, power, remainder)
    func_sign_list = ("+", "-", "*", "/", "^", "%")
    
    def get_input(text_label, type):
        while True:
            i = input(f"{text_label} : ")
            if i in ["#", "$"]:
                raise Exception(i)
            elif type == "OP":
                if i in func_sign_list:
                    return i
                else:
                    print("Please enter a valid operation ")
            elif type == "NUM":
                try:
                    return float(i)
                except:
                    print("Please enter a  valid number ")
                    continue
    
    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    : $ ")
    
        # process
        try:
            choice = get_input("Enter choice(+,-,*,/,^,%,#,$)", "OP")
            func_sign_index = func_sign_list.index(choice)
            if func_sign_index < len(func_list):
                func_ = func_list[func_sign_index]
                a = get_input("Enter First Number", "NUM")
                b = get_input("Enter Second Number", "NUM")
                res = func_(a, b)
                print(a, choice, b, "=", res)
        except Exception as e:
            if str(e) == "$":
                continue
            elif str(e) == "#":
                break

    මේක සබ්මිට් කරන්න එපා. මම මෙ නිකන් ලිව්වෙ. මොනව හරි ගන්න දෙයක් තියේ නන් ගන්න. වැරද්දක් තියේ නන් කියන්න
     

    IT kolla

    Active member
  • Mar 8, 2022
    171
    168
    43
    Reset එකත් choice එකේ දැම්මොත් විතරයි වැඩ
    ow machan, meka thama awla thibbe. mama wena wenama input walata demmama code eka weda kala. help krpu hemotama thanks. eka weda kala. EOFError eka hari giya except ekata valueerror keyword eka demmama.
    Python:
    # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        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

    Python:
    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 remainder(x, y): return x % y
    def power(x, y): return x**y
    
    func_list = (add, substract, multiply, divide, power, remainder)
    func_sign_list = ("+", "-", "*", "/", "^", "%")
    
    def get_input(text_label, type):
        while True:
            i = input(f"{text_label} : ")
            if i in ["#", "$"]:
                raise Exception(i)
            elif type == "OP":
                if i in func_sign_list:
                    return i
                else:
                    print("Please enter a valid operation ")
            elif type == "NUM":
                try:
                    return float(i)
                except:
                    print("Please enter a  valid number ")
                    continue
    
    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    : $ ")
    
        # process
        try:
            choice = get_input("Enter choice(+,-,*,/,^,%,#,$)", "OP")
            func_sign_index = func_sign_list.index(choice)
            if func_sign_index < len(func_list):
                func_ = func_list[func_sign_index]
                a = get_input("Enter First Number", "NUM")
                b = get_input("Enter Second Number", "NUM")
                res = func_(a, b)
                print(a, choice, b, "=", res)
        except Exception as e:
            if str(e) == "$":
                continue
            elif str(e) == "#":
                break

    මේක සබ්මිට් කරන්න එපා. මම මෙ නිකන් ලිව්වෙ. මොනව හරි ගන්න දෙයක් තියේ නන් ගන්න. වැරද්දක් තියේ නන් කියන්න
    thanks mchan. mama meka balannm. kohomath mama answer eka submit kala.
    ------ Post added on May 8, 2022 at 12:33 AM
     
    • Love
    Reactions: olu bakka

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    48,497
    98,447
    113
    ow machan, meka thama awla thibbe. mama wena wenama input walata demmama code eka weda kala. help krpu hemotama thanks. eka weda kala. EOFError eka hari giya except ekata valueerror keyword eka demmama.
    Python:
    # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        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


    thanks mchan. mama meka balannm. kohomath mama answer eka submit kala.
    ------ Post added on May 8, 2022 at 12:33 AM
    repeat code nathi karala liwe.
     

    olu bakka

    Well-known member
  • Aug 18, 2011
    22,013
    22,421
    113
    ow machan, meka thama awla thibbe. mama wena wenama input walata demmama code eka weda kala. help krpu hemotama thanks. eka weda kala. EOFError eka hari giya except ekata valueerror keyword eka demmama.
    Python:
    # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        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


    thanks mchan. mama meka balannm. kohomath mama answer eka submit kala.
    ------ Post added on May 8, 2022 at 12:33 AM
    Niyamayi bro

    Python:
    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 remainder(x, y): return x % y
    def power(x, y): return x**y
    
    func_list = (add, substract, multiply, divide, power, remainder)
    func_sign_list = ("+", "-", "*", "/", "^", "%")
    
    def get_input(text_label, type):
        while True:
            i = input(f"{text_label} : ")
            if i in ["#", "$"]:
                raise Exception(i)
            elif type == "OP":
                if i in func_sign_list:
                    return i
                else:
                    print("Please enter a valid operation ")
            elif type == "NUM":
                try:
                    return float(i)
                except:
                    print("Please enter a  valid number ")
                    continue
    
    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    : $ ")
    
        # process
        try:
            choice = get_input("Enter choice(+,-,*,/,^,%,#,$)", "OP")
            func_sign_index = func_sign_list.index(choice)
            if func_sign_index < len(func_list):
                func_ = func_list[func_sign_index]
                a = get_input("Enter First Number", "NUM")
                b = get_input("Enter Second Number", "NUM")
                res = func_(a, b)
                print(a, choice, b, "=", res)
        except Exception as e:
            if str(e) == "$":
                continue
            elif str(e) == "#":
                break

    මේක සබ්මිට් කරන්න එපා. මම මෙ නිකන් ලිව්වෙ. මොනව හරි ගන්න දෙයක් තියේ නන් ගන්න. වැරද්දක් තියේ නන් කියන්න
    මේ f string මාර පහසුයි නේද බන්. මම ලගදි එහෙම එකක් තියෙනව කියල දැනගත්තෙ.
    ------ Post added on May 8, 2022 at 12:46 AM
     

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    48,497
    98,447
    113
    Niyamayi bro


    මේ f string මාර පහසුයි නේද බන්. මම ලගදි එහෙම එකක් තියෙනව කියල දැනගත්තෙ.
    ------ Post added on May 8, 2022 at 12:46 AM
    ඔව් ප්ය්තන් වල ගොඩක් සපොට් තියෙනව ඔය වගේ ඒවට, තව ලිස්ට් වලින් එහෙම කරන්න පුලුවන් වැඩ ගොඩක් තියෙනව
     
    • Love
    Reactions: olu bakka

    KGVA

    Member
    Jul 15, 2022
    2
    2
    3
    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

    WARNING

    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")
    ------ Post added on Jul 22, 2022 at 4:31 PM
     

    janidu malli

    Member
    Jan 25, 2024
    1
    0
    1
    ow machan, meka thama awla thibbe. mama wena wenama input walata demmama code eka weda kala. help krpu hemotama thanks. eka weda kala. EOFError eka hari giya except ekata valueerror keyword eka demmama.
    Python:
    # take input from the user
      choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
      print(choice)
      if choice == "#":
        #program ends here
        print("Done. Terminating")
        exit()
      if choice == "$":
        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


    thanks mchan. mama meka balannm. kohomath mama answer eka submit kala.
    ------ Post added on May 8, 2022 at 12:33 AM
    ee ayiye matath oyaa gahapu full python code tika evannako