Prabhu
Prabhu

Reputation: 17

Python - Else condition not executed

Please check my code below, in that else condition matches (test data is "k"), but not executing the statement inside it, rather it gives error like -

NameError: name "k" is not defined.

at the same time if I give 8 or 9, it did not show the above error at the same time not executed the else statement as well, just showing the main menu again

from math import sqrt
def addition(firstNum,secondNum):
    total = firstNum+secondNum
    return total

def substraction(firstNum,secondNum):
    total = firstNum-secondNum
    return total

def multiplication(firstNum,secondNum):
    total = firstNum*secondNum
    return total

def division(firstNum,secondNum):
    total = firstNum/secondNum
    return total

def root(firstNum):
    total = (firstNum*firstNum)
    return total

menu={}
menu[" "] = "          --------PYTHON CALCULATOR-------            "
menu["."] = "~~Main Menu~~"
menu["1"] = "Addition"
menu["2"] = "Substraction"
menu["3"] = "Multiplication"
menu["4"] = "Division"
menu["5"] = "Square"
menu["6"] = "Root"
menu["7"] = "Exit\n"

while True:
    options = menu.keys()
    options.sort()
    for entry in options:
        print entry, menu[entry]

    selection = input("Please select your correct option: ")

    if selection   == 1:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")

        print "Answer is " , addition(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 2:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is " ,substraction(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 3:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is ",multiplication(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 4:
        firstNum=input ("\nPlease enter first number : ")
        secondNum=input("Please enter Second number: ")
        print "Answer is " ,division(firstNum,secondNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 5:
        firstNum=input ("\nPlease enter first number : ")
        print "Answer is " ,sqrt(firstNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 6:
        firstNum=input ("\nPlease enter first number : ")
        print "Answer is " ,root(firstNum)
        print "--------------------------------------------------------\n\n"
    elif selection == 7:
        break
    else:
        "Invalid option keyed in..."

Upvotes: 0

Views: 105

Answers (1)

user3046287
user3046287

Reputation:

Use raw_input instead of input. Here's why. raw_input return the string entered by the user, you should then convert to int using:

firstnum = int(raw_input('...'))

Upvotes: 1

Related Questions