Hello Daniweb,

I'm beginning to learn Python, which I hope shall be a useful tool to help with server administration and the such.

I am however having some problems, I'm sure this is a very simple problem but I can't see what is going wrong.

#!/usr/bin/env python3.3

validSelection = 0

print("Welcome to the Python Calculator V1.0 \n")

print("Please choose what operator you would like to use: \n")

print("1. Addition")
print("2. Division")
print("3. Subtraction")
print("4. Multiplication \n")

while(validSelection != 1):

    menuSelection = input("Please Make Your Selection: ")

    if(menuSelection == 1):

        validSelection = 1

        Val1 = input("Please Enter Value 1: ")
        Val2 = input("Please Enter Value 2: ")

        Addition(Val1, Val2)

    elif(menuSelection == 2):

        validSelection = 1

        Val1 = input("Please Enter Value 1: ")
        Val2 = input("Please Enter Value 2: ")

        Division(Val1, Val2)

    elif(menuSelection == 3):

        validSelection = 1

        Val1 = input("Please Enter Value 1: ")
        Val2 = input("Please Enter Value 2: ")

        Subtraction(Val1, Val2)

    elif(menuSelection == 4):

        validSelection = 1

        Val1 = input("Please Enter Value 1: ")
        Val2 = input("Please Enter Value 2: ")

        Multiplication(Val1, Val2)

    else:

        print("\nThat Selection is Not Valid! \n")

This is only part of the calculator, but it always displays as an invalid selection. If I type in 1, 2, 3 or 4 (which should be valid) it shall always shoot to the else part of my if statement.

Any help would be greatly appreciated.
Thank you!

Nevermind, I worked out it was taking the user input as a string.

I needed to do this:

int(input("Please Enter Your Selection: "))

You also need some way to exit the while loop.

Some repating code like Val1,Val2 is not so god.
You should make a funcion for this and send function/operator in as argument.

Maybe this help,here is how i would write this.
I use a dictionary with lambda for operators to shorten the need for 4 function,Addition(),Multiplication()....

def showoptions():
     print("1. Addition")
     print("2. Division")
     print("3. Subtraction")
     print("4. Multiplication\n")

def get_option():
    ops = {'1': lambda x, y: x + y, '4': lambda x, y: x * y,
           '2': lambda x, y: x / y, '3': lambda x, y: x - y}
    while True:
        showoptions()
        option = input("Please Make Your Selection: ")
        if option in ops:
            return ops[option]
        else:
            print("'{}' is not a valid option,Try again\n".format(option))

def calculate(operator):
    val1 = float(input("Please Enter Value 1: "))
    val2 = float(input("Please Enter Value 2: "))
    return operator(val1,val2)

def main():
    operator = get_option()
    print('Result is: {}'.format(calculate(operator)))

if __name__ == '__main__':
    main()

Thanks for the suggestions, and the code example, I shall look into it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.