pythonfunctionmultiple-choice

basic calculator, multiple input and choice through functions


as you can tell by my code, i want to make a basic calculator, you get asked what you want to do first then asked 2 input 2 numbers to be worked out by the code. I am having a problem where the code returns nothing and doesnt even attempt to use the functions it is meant to.

##Simple Calculator program##
print("Welcome to my basic calculator program")

print("In this program you will be asked to input what function you want to do then")
print("select 2 numbers, where the program will then do the mathematic operation on those 2 numbers")
#Class containing the functions and basic caluclation
 
class calculator_class():
    print("Please select a function by entering these :")
    print("Addition")
    print("Subtraction")
    print("Multiplication")
    print("Division")
    #this is a function which asks the user to choose what operator to choose before choosing their number
    
    def userchoice():
        
        userchoices = str(input())
        if userchoices in ["Addition","Subtraction","Multiplication","Division"]:
            return(
        
        
        if userchoices == "Addition":
            print(addition())
        elif userchoices == "Subtraction":
            print(subtraction())
        elif userchoices == "multiplication":
            print(multiplication())
        elif userchoices == "division":
            print(division())
        else:
            print(invalid_choice())
    print(userchoice())
    #here the user chooses the 2 numbers
    print("Please select 2 numbers to calculate")
        
    usernumber1 = int(input("Please input your first number here  : "))
    usernumber2 = int(input("Please input your second number here : "))
        
    #Functions of which contain addition, subtraction, multiplication and division
    def addition():
        print("A D D I T I O N")
        print("Just calculating...")
        
        print(usernumber1 + usernumber2)
        
    
    def subtraction():
        print("S U B T R A C T I O N")
        print("Just calculating...")
        print(usernumber1 - usernumber2)
        
    
    def multipliction():
        print("M U L T I P L I C A T I O N ")
        print("Just calculating...")
        print(usernumber1 * usernumber2)
        
    
    def division():
        print("D I V I S I O N ")
        print("Just calculatin...")
        print(usernumber1 / usernumber2)
        
        
    def invalid_choice():
        print("You did not pick a valid option, please try again")
    

Solution

  • You have many wrong approaches in this code.

    1. More easier to type just a one sign (* for example) instead of typing "multiplication"
    2. It is better to apply .lower() for every user input
    3. input in python is always str, so str() in userchoices = str(input()) is redundant
    4. int() for input() may lead to error (int('1.2') # error), so put such code in try/except block