Jump to content

[PYTHON] Easy Calculator


Tupi
 Share

Recommended Posts

Code:

def add(x, y): #functie pentru adunat cele doua numere
    return x + y
def substract(x, y): #functie pentru scazut cele doua numere
    return x - y
def multiply(x, y): #functie pentru inmultit cele doua numere
    return x * y
def divide(x, y): #functie pentru impartit cele doua numere
    return x / y
   
    
print("Select an operation from below:")
print ("1. Add numbers")
print ("2. Substract numbers")
print ("3. Multiply numbers")
print ("4. Divide numbers")
    
while True:
    selected = input("Enter you operation number (1/2/3/4): ") #selected - tine valoarea operatiei introduse in terminal
    
    if selected in ('1', '2', '3', '4'): #daca numarul operatiei alese este unul corect
        num1 = float(input("Enter your first number: ")) #retinem primul numar
        num2 = float(input("Enter your second number: ")) #retinem al doilea numar
        
        if selected == '1': #daca am ales prima operatie le vom aduna
            print(num1, "+", num2, "=", add(num1,num2))
        elif selected == '2': #daca am ales prima operatie le vom scadea
            print(num1, "-", num2, "=", substract(num1,num2))
        elif selected == '3': #daca am ales prima operatie le vom inmulti
            print(num1, "x", num2, "=", multiply(num1,num2))
        elif selected == '4': #daca am ales prima operatie le vom imparti
            print(num1, ":", num2, "=", divide(num1,num2))
            
        next = input("* Do you want to make other operation? (Y/N)") #intrebam daca vrem sa mai efectuam alta operatie | raspuns salvat in next
        if next == "N": #daca am ales n sau N scriptul se opreste
            break
        if next == "n":
            break
        
    else: #daca operatia aleasa nu se regaseste atunci afisam o eroare
        print("Operation number is invalid. Please try again!")

 

Output:

Select an operation from below:
1. Add numbers
2. Substract numbers
3. Multiply numbers
4. Divide numbers
Enter you operation number (1/2/3/4): 4
Enter your first number: 500
Enter your second number: 600
500.0 : 600.0 = 0.8333333333333334
* Do you want to make other operation? (Y/N)n

 

Link to comment
Share on other sites

  • Tupi locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.