Python Code help For Implement a list

shw7

Well-known member
  • May 30, 2009
    2,150
    171
    113
    London, England

    Python:
    result = 0
    
    def add(num1, num2):
        result = num1 + num2
        return result
       
    def subtract(num1, num2):
        result = num1 - num2
        return result
       
    def multiply(num1, num2):
        result = num1 * num2
        return result
       
    def divide(num1, num2):
        try:
            result = num1 / num2
        except ZeroDivisionError:
            print('float division by zero')
            result = None        
           
        return result
       
    def power(num1, num2):
        result = num1 ** num2
        return result
       
    def remainder(num1, num2):
        result = num1 % num2
        return result
    
    def select_op(choice):
        if choice == '#':
            return -1
           
        else:
            number_one = input("Enter first number: ")
            print(number_one)
            if number_one.endswith('$'):
                return 0
            elif number_one == '#':
                return -1
            else:
                number_two = input("Enter second number: ")
                print(number_two)
                if number_two.endswith('$'):
                    return 0
                elif number_two == '#':
                    return -1
                         
            number_one_float = float(number_one)
            number_two_float = float(number_two)
           
            if choice == '+':
                result = add(number_one_float, number_two_float)
            elif choice == '-':
                result = subtract(number_one_float, number_two_float)
            elif choice == '*':
                result = multiply(number_one_float, number_two_float)
            elif choice == '/':
                result = divide(number_one_float, number_two_float)
            elif choice == '^':
                result = power(number_one_float, number_two_float)
            elif choice == '%':
                result = remainder(number_one_float, number_two_float)
    
            print(str(number_one_float) + " " + choice + " " + str(number_two_float) + " = " + str(result))
    
    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()
    ------ Post added on Apr 13, 2022 at 2:07 PM
     
    Nov 13, 2022
    1
    0
    1
    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
    def get_user_input():
    a = input("Enter first number: ")
    print(a)
    if(a=='#'):
    return -1
    try:
    fa=float(a)
    except ValueError:
    return None
    b = input("Enter second number: ")
    print(b)
    if(b=='#'):
    return -1
    try:
    fb=float(b)
    except ValueError:
    return None
    return fa,fb

    def add(a,b):
    print(float(a), "+", float(b), "=", float(a)+float(b))
    return

    def subtract(a,b):
    print(float(a), "-", float(b), "=", float(a)-float(b))

    def multiply(a,b):
    return a*b

    def divide(a,b):
    try:
    print(float(a), "/", float(b), "=", float(a)/float(b))
    except ZeroDivisionError:
    print("float division by zero")
    print(float(a), "/", float(b), "=", "None")
    except ValueError:
    pass
    return

    def power(a,b):
    return a+b

    def remainder(a,b):
    return a+b


    def select_op(choice):
    if(choice == "#"):
    return -1
    elif(choice == "+"):
    Z = get_user_input()
    if(Z==None):
    return
    elif(Z==-1):
    return -1
    a,b = Z
    add(a,b)
    elif(choice == "-"):
    Z = get_user_input()
    if(Z==None):
    return
    elif(Z==-1):
    return -1
    a,b = Z
    subtract(a,b)
    elif(choice == "/"):
    Z = get_user_input()
    if(Z==None):
    return
    a,b = Z
    divide(a,b)



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